c# - 使用授权属性时出现 NullReferenceException

标签 c# asp.net-mvc-3

我在 HomeController 上有 [Authorize] 属性,每当我尝试访问它时,它都会抛出一个 NullReferenceException

这真的有点奇怪,因为我以前多次使用 [Authorize] 并且它工作得很好。在这种情况下唯一的区别是此应用程序托管在我们自己的使用 Windows 7 和 IIS 7.5 的 Web 服务器上

这是堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.]
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 filters, ActionDescriptor actionDescriptor) +155
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String 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 step, Boolean& completedSynchronously) +371

编辑:

在查看 AuthorizeCore 方法的代码时,似乎 AuthorizeCore 正在抛出 NullReferenceException 因为它得到一个 NULL HttpContextBase.

这可能吗?因为应用程序中的其他一切都工作正常,比如访问数据库、创建 auth cookie 等。

编辑 2:

在将其发布到网络服务器之后发生。在开发过程中,它在 Visual Studio 中运行得非常好。

最佳答案

问题更糟HttpContext在 Controller 和 Razor View 中甚至不可用。因此,我使用 aspnet_regiis -ir 重新安装了 ASP.NET v4.0 .然后用ASP.NET 4.0在注册期间创建的池而不是使用 DefaultAppPool .

它开始正常工作。它还解决了我的另一个重写问题 <modules runAllManagedModulesForAllRequests="true"/>在我的应用程序 web.config 中。

关于c# - 使用授权属性时出现 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7159791/

相关文章:

c# - asp.net 用户控件属性

c# - 干净利落地中断 HttpListener 的 BeginGetContext 方法

c# - 我如何使用和保存 View 模型

asp.net-mvc-3 - 如何正确使用 RedirectToAction

c# - 如何读取 Windows 应用商店应用程序中的二进制文件?

C# 使用 : constructor in a different method

c# - 更好的代码 : Extend LINQ class, 或创建单独的助手类?

asp.net-mvc - 使用 HTML.BeginForm 时,ReturnUrl 如何存储在 MVC4 Razor 页面中?

asp.net-mvc-3 - 使用 Jquery Ajax 和可变数据渲染部分 View

c# - 如何防止 ToString ("0,0") 返回 00