c# - 添加 'HttpGet' 命名空间时找不到类型或命名空间名称 'System.Web.Http'

标签 c# asp.net-mvc http-post http-get

我在 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/

相关文章:

c# - jQuery .load 似乎在意想不到的地方运行

c# - Where(condition).Any() 和 Any(condition) 是否等价

c# - Ghostscript.NET 如何处理 StdIn 上的输入

jquery - 仅在一页上通过 AJAX POST 获取 NTLM 挑战

forms - History.pushstate 和 HTTP POST 表单转发

javascript - 浏览器是否会在 AJAX 响应中自动处理 HTTP header ?

java - 将 Angular JS 中的数据作为表单数据发布到 Java REST Web 服务时出现问题

c# - 文件/图像上传后 ASP.NET 3.0 mvc 应用程序崩溃

jquery - 使用 jquery post 将字典数据传递给 Controller ​​字符串方法

c# - 从头开始为商业应用预订 ASP.NET MVC