添加'System.Web.Http'命名空间时,找不到类型或命名空间名称'HttpGet' [英] The type or namespace name 'HttpGet' could not be found when add 'System.Web.Http' namespace

查看:486
本文介绍了添加'System.Web.Http'命名空间时,找不到类型或命名空间名称'HttpGet'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC中有一个问题。

I have one issue in MVC .

目前我在MVC工作,版本是MVC4。我有2个ActionResult方法,见下文

Currently I am working in MVC and the version is MVC4 . And I have 2 ActionResult Method, see below

[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();
        }

我们需要使用System.Web.Mvc ; [HttpPost] [HttpGet] 属性的命名空间。所以我在控制器中使用System.Web.Mvc; 命名空间添加了。但我需要在我的控制器中使用System.Web.Http; httpsresponseexpection 错误处理添加另一个命名空间。我在命名空间中添加了。目前 System.Web.Mvc; 无效。

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 httpsresponseexpection error handling in my controller .Si I added in the namespace . At this time System.Web.Mvc; is not working .


我收到此错误:无法找到类型或命名空间名称HttpGet。为什么?用于HttpGet的System.Web.Mvc和System.Web.Http之间的任何关系?

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 类:

The reason you are getting this exception is because there are 2 different HttpGetAttribute classes in 2 different namespaces:

  • System.Web.Mvc.HttpGetAttribute
  • System.Web.Http.HttpGetAttribute

第一个用于ASP.NET MVC控制器,第二个用于ASP.NET Web API控制器。

The first is used in ASP.NET MVC controllers and the second is used in ASP.NET Web API controllers.

当您导入第二个命名空间时,编译器不再能够消除您引用的2个类中的哪个类的歧义,因为2个命名空间在范围内。

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.

基本上微软复制了所有ASP.NET MVC中存在的用于Web API的类,但将它们放在不同的命名空间中。基本上你不应该混合这些命名空间。

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.


但我需要使用System.Web.Http添加另一个命名空间; for
我的控制器中的httpsresponseexpection错误处理

But i need to add another one Namespace using System.Web.Http; for httpsresponseexpection error handling in my controller

为什么需要在ASP.NET MVC控制器中使用它?通常这是你应该在Web API控制器中做的事情。

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.

但是如果由于某种原因你需要混合2你必须明确指定你需要哪个属性通过完全限定使用它:

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:

[System.Web.Mvc.HttpGet]
public ActionResult About()
{
    ViewBag.Message = "Your app description page.";
    return View();
}

这篇关于添加'System.Web.Http'命名空间时,找不到类型或命名空间名称'HttpGet'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆