我在 MVC 中遇到一个问题。
目前我在 MVC 工作,版本是 MVC4 。我有 2 个 ActionResult 方法,见下文
[HttpGet]
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
[HttpPost]
public ActionResult About(ModelName ccc)
{
ViewBag.Message = "Your app description page.";
return View();
}
[HttpPost] 和 [HttpGet] 属性需要 using System.Web.Mvc;
命名空间。所以我在我的 Controller 中添加了 using System.Web.Mvc;
命名空间。但是我需要添加另一个命名空间 using System.Web.Http;
用于在我的 Controller 中处理 httpsresponseexpection 错误。我在命名空间中添加了。此时 System.Web.Mvc;
不工作。
I got this error: The type or namespace name 'HttpGet' could not be found . Why ? anything relation between System.Web.Mvc and System.Web.Http for HttpGet ?
最佳答案
您收到此异常的原因是因为在 2 个不同的命名空间中有 2 个不同的 HttpGetAttribute
类:
第一个用于 ASP.NET MVC Controller ,第二个用于 ASP.NET Web API Controller 。
当您导入第二个命名空间时,编译器不再能够区分您引用的 2 个类中的哪一个,因为这 2 个命名空间在范围内。
基本上,Microsoft 为 Web API 复制了 ASP.NET MVC 中存在的所有类,但将它们放在不同的命名空间中。基本上你不应该混合这些命名空间。
But i need to add another one Namespace using System.Web.Http; for httpsresponseexpection error handling in my controller
为什么需要在 ASP.NET MVC Controller 中使用它?通常这是您应该在 Web API Controller 中执行的操作。
但是如果出于某种原因你需要混合这 2 个,你将必须通过完全限定来明确指定你需要使用哪个属性:
[System.Web.Mvc.HttpGet]
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
关于c# - 添加 'HttpGet' 命名空间时找不到类型或命名空间名称 'System.Web.Http',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19311044/