C# 添加“System.Web.Http”命名空间时找不到类型或命名空间名称“HttpGet”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19311044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The type or namespace name 'HttpGet' could not be found when add 'System.Web.Http' namespace
提问by Ramesh Rajendran
I have one issue in MVC .
我在 MVC 中有一个问题。
Currently I am working in MVC and the version is MVC4 . And I have 2 ActionResult Method, see below
目前我在 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();
}
We need the using System.Web.Mvc;
namespace for [HttpPost]and [HttpGet]attributes. So I added the using System.Web.Mvc;
namespace in my controller . But i need to add another one Namespace using System.Web.Http;
for httpsresponseexpectionerror handling in my controller .Si I added in the namespace . At this time System.Web.Mvc;
is not working .
我们需要[HttpPost]和[HttpGet]属性的using System.Web.Mvc;
命名空间 。所以我在我的控制器中添加了命名空间。但是我需要在我的控制器中添加另一个 用于httpsresponseexpection错误处理的命名空间 .Si 我在命名空间中添加。这时候是不行的。using System.Web.Mvc;
using System.Web.Http;
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 ?
我收到此错误:找不到类型或命名空间名称“HttpGet”。为什么 ?System.Web.Mvc 和 System.Web.Http for HttpGet 之间有什么关系?
采纳答案by Darin Dimitrov
The reason you are getting this exception is because there are 2 different HttpGetAttribute
classes in 2 different namespaces:
您收到此异常的原因是因为HttpGetAttribute
在 2 个不同的命名空间中有 2 个不同的类:
The first is used in ASP.NET MVC controllers and the second is used in ASP.NET Web API controllers.
第一个用于 ASP.NET MVC 控制器,第二个用于 ASP.NET Web API 控制器。
When you imported the second namespace the compiler is no longer able to disambiguate which of the 2 classes you are referring to because the 2 namespaces are in scope.
当您导入第二个命名空间时,编译器不再能够区分您所引用的 2 个类中的哪一个,因为这 2 个命名空间都在范围内。
Basically Microsoft duplicated all the classes that existed in ASP.NET MVC for the Web API but placed them in different namespace. Basically you shouldn't be mixing those namespaces.
基本上,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
但我需要使用 System.Web.Http 添加另一个命名空间;用于我的控制器中的 httpsresponseexpection 错误处理
Why would you need to use this in an ASP.NET MVC controller? Normally that's something you should be doing in a Web API controller.
为什么需要在 ASP.NET MVC 控制器中使用它?通常,这是您应该在 Web API 控制器中执行的操作。
But if for some reason you need to mix the 2 you will have to explicitly specify which attribute you need to use by fully qualifying it:
但是如果由于某种原因你需要混合 2 你必须通过完全限定它来明确指定你需要使用哪个属性:
[System.Web.Mvc.HttpGet]
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
回答by Bill DeRose
I ran into this problem on OS X using .NET Core. I was missing an entry for Microsoft.AspNetCore.Mvc
in my project.json
.
我在使用 .NET Core 的 OS X 上遇到了这个问题。我Microsoft.AspNetCore.Mvc
在我的project.json
.
Before:
前:
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
...,
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0"
},
...
}
After:
后:
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
...,
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0"
},
...
}
回答by fordareh
For the record, I had a similar issue in a class library with a reference to MVC. I installed a nuget package in the class library that itself had a dependency on MVC (note: the new package was: RazorGenerator.Mvc). Previously, the class library relied on a Sytem.Web.Mvc.dll reference added using the 'Add Reference' dialogue rather than on one installed via the "Microsoft.AspNet.Mvc" nuget package.
作为记录,我在引用 MVC 的类库中遇到了类似的问题。我在类库中安装了一个 nuget 包,它本身依赖于 MVC(注意:新包是:RazorGenerator.Mvc)。以前,类库依赖于使用“添加引用”对话框添加的 Sytem.Web.Mvc.dll 引用,而不是通过“Microsoft.AspNet.Mvc”nuget 包安装的引用。
Nuget chose the lowest MVC version that would meet the dependency requirements of the new package. That meant that the previous reference (to MVC 5) was replaced with a lower version reference (to MVC 3).
Nuget 选择了可以满足新包依赖项要求的最低 MVC 版本。这意味着之前的参考(对 MVC 5)被替换为较低版本的参考(对 MVC 3)。
Upgrading the MVC version in the class library using nuget resolved the issue.
使用 nuget 升级类库中的 MVC 版本解决了该问题。
回答by Chandni Dimri
Here is solution for that problem try it....
这是该问题的解决方案试试吧....
[System.Web.Mvc.HttpGet]
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}