对类型"HttpContextBase"的引用声称它是在"System.Web"中定义的,但找不到 [英] Reference to type 'HttpContextBase' claims it is defined in 'System.Web' but it could not be found

查看:104
本文介绍了对类型"HttpContextBase"的引用声称它是在"System.Web"中定义的,但找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常奇怪的错误,通过一些相当广泛的谷歌搜索,我只发现了几次.我正在创建一个授权属性,以将其添加到类库的ASP.NET MVC项目中的Actions上.我通过NuGet安装了ASP程序包,并且可以使用intellisense从filterContext中获取HttpContext,但这是说HttpContext类型在应有的位置找不到.

I'm getting a really weird error that I've only found a few times with some pretty extensive googling. I'm making an authorization attribute to add onto my Actions in an ASP.NET MVC project in a class library. I installed the ASP packages through NuGet and can use intellisense to get the HttpContext from the filterContext, but it's saying that the HttpContext type can't be found where it's supposed to be.

引用类型'HttpContextBase'声称它是在'System.Web'中定义的,但是找不到.

Reference to type 'HttpContextBase' claims it is defined in 'System.Web' but it could not be found.

StackOverflow上有几个版本,但是它们都不适合我,因为其中大多数包含重新启动Visual Studio或仅构建项目.

There's a few versions of this on StackOverflow, but none of them worked for me, as most of them consisted of restarting Visual Studio or just building the project.

编辑

using System.Web.Mvc;
namespace Foo
{
    public class RequireLogin : IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext.HttpContext.Request.Cookies)
            {

            }
        }
    }
}

错误发生在if语句的HttpContext上.

The error is on HttpContext on the if statement.

推荐答案

我在一个相当简单的Web页面应用程序中遇到了相同的错误,该应用程序由一个单独的 .cshtml 页面组成,什么都没有("Hello World").就我而言,该错误是由于引用了错误的.NET版本造成的.

I was getting the same error in a rather simple Web Pages application that consisted of a single .cshtml page, which was doing absolutely nothing ("Hello World"). In my case the the error was a result of an incorrect .NET version being referenced.

显然,如果您只是从头开始创建基本的 web.config index.cshtml ,则默认引用.NET版本2,因此 HttpContextBase 未定义.

Apparently, if you just create a rudimentary web.config and index.cshtml from scratch, .NET version 2 is referenced by default and therefore HttpContextBase is not defined.

将目标版本更改为.NET 4.x,问题已解决.

Change the target version to .NET 4.x and the problem is resolved.

我能够使用的最简单的 web.config 是:

The simplest web.config I was able to get it to work with is:

<?xml version="1.0"?>
<configuration>
  <system.webServer>
    <defaultDocument>
      <files>
        <add value="index.cshtml"/>
      </files>
    </defaultDocument>
  </system.webServer>
  <system.web>
    <compilation targetFramework="4.6.2" debug="true"/>
  </system.web>
</configuration>

摘要,请检查您的.NET目标版本.很有可能是错误的.

In summary, check your .NET target version. It most likely is incorrect.

这篇关于对类型"HttpContextBase"的引用声称它是在"System.Web"中定义的,但找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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