问题描述
我 [授权]
在的HomeController
,每当我试图访问它,它抛出一个属性的NullReferenceException
I have [Authorize]
attribute on the HomeController
, whenever I am trying to access it, it throws a NullReferenceException
这真是一种奇怪的,因为我以前使用 [授权]
很多次,它工作得很好。只有在这种情况下,不同的是此应用程序的我们自己的网站使用的是Windows 7和服务器托管; IIS 7.5
This is really kind of weird, because I have used [Authorize]
many times before and it works just fine. Only difference in this case is this Application is hosted on our own Web Server using Windows 7 & IIS 7.5
下面是堆栈跟踪:
[NullReferenceException:未将对象引用未设置为一个实例
对象。]结果
System.Web.Mvc.AuthorizeAttribute.AuthorizeCore(HttpContextBase
HttpContext的)+38结果
System.Web.Mvc.AuthorizeAttribute.OnAuthorization(AuthorizationContext
filterContext)+160结果
System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext
controllerContext,IList`1过滤器,ActionDescriptor actionDescriptor)
+155结果
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext
controllerContext,字符串actionName)784976结果
System.Web.Mvc.Controller.ExecuteCore()+159结果
System.Web.Mvc.ControllerBase.Execute(RequestContext的的RequestContext)
335 System.Web.Mvc<> C_的 DisplayClassb.b 的_5()
+62结果
System.Web.Mvc.Async<> C_的 DisplayClass1.b 的_0()+20结果
System.Web.Mvc<> C_的 DisplayClasse.b 的_D()+54结果
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
453 System.Web.HttpApplication.ExecuteStep(IExecutionStep一步,
布尔和放大器; completedSynchronously)+371
编辑:
虽然寻找到 AuthorizeCore
方法code,似乎 AuthorizeCore
抛出的NullReferenceException
,因为它得到一个NULL HttpContextBase
。
While looking into code of AuthorizeCore
method, it seems that AuthorizeCore
is throwing NullReferenceException
because it gets a NULL HttpContextBase
.
难道是可能的吗?因为一切都在其他应用程序中工作得很好,比如访问数据库,创建AUTH饼干等。
Could it be possible? Because everything else in application is working just fine, like accessing database, creating auth cookie etc.
编辑2:
这发生的仅发布到Web服务器。虽然发展,它的作品精美绝伦从Visual Studio。
This happen only after publishing it to Web Server. While development, it works absolutely fine from Visual Studio.
推荐答案
问题更是雪上加霜的HttpContext
是控制器的和剃刀观点甚至没有用。所以,我重新安装使用为aspnet_regiis -ir
ASP.NET v4.0的。然后用其注册时产生的,而不是使用 ASP.NET 4.0
池默认应用
。
Issue was even more worse HttpContext
was not even available in Controller's and Razor views. So, I reinstall ASP.NET v4.0 using aspnet_regiis -ir
. And then used ASP.NET 4.0
pool which was created during registration instead of using DefaultAppPool
.
和它开始的做工精细。这也解决了我重写的另一个问题<模块runAllManagedModulesForAllRequests =真/方式>
在我的应用程序的web.config
And it started working fine. It also solve my another issue of overriding <modules runAllManagedModulesForAllRequests="true"/>
in my application web.config.
这篇关于的NullReferenceException同时使用授权属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!