代码之家  ›  专栏  ›  技术社区  ›  Alex

对于不存在的静态文件,HttpContext.User在BeginExecute方法处为空

  •  0
  • Alex  · 技术社区  · 6 年前

    这个标题搞砸了,但我盯着它看了10分钟,真的不知道该怎么说得更好。

    抽象时引发异常 BaseAuthorizedController ,装饰有 [Authorize] BeginExecute 重写的方法。

    代码行如下所示:

    var userId = requestContext.HttpContext.User.Identity.GetUserId();
    

    请求是关于一个不再存在的静态文件。稍后再谈。

    Object reference not set to an instance of an object. ,堆栈跟踪:

    [NullReferenceException: Object reference not set to an instance of an object.]
       <namespace>.BaseAuthorizedController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) in <path>\Controllers\BaseAuthorizedController.cs:35
       System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16
       System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52
       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
       System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
       System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +369
       System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
       System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103
       System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159
    

    HttpContext.User 对象为空。这很有意义,因为它是一个简单的静态文件请求。我不明白的是为什么 方法首先被调用,当 控制器处理请求?我相信没有控制者 基本授权控制器 ,它处理此请求。

    不存在 文件是根据 目录。

    1. localhost:1000/non-existing-file.txt -回报 404 .
    2. localhost:1000/files/existing-file.txt
    3. localhost:1000/files/not-existing-file -回报 404个
    4. localhost:1000/files/not-existing-file.txt -电话 开始执行 , User 为空,响应为 500

    我收集到的信息:

    1. 只有当文件扩展名存在时才会发生这种情况- . 后面跟着任何符号
    2. 似乎只发生在 /files/ 。没有 FilesController ,仅 Files 目录,包含静态文件。也没有区域或自定义路由映射到 /files/* 基本授权控制器 .

    你知道什么会导致这种行为吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Alex    6 年前

    原来这是 IIS 问题:

    1. 路由-在我的项目中 FillesController ,但是它是在一个单独的 面积 ,因此不应与此路线匹配。但是,它确实是,这导致了空引用异常。
    2. IIS-我通过添加 runAllManagedaModulesForAllRequests="true" Web.config - modules . 下面是它现在的样子:

      <modules runAllManagedModulesForAllRequests="true"> <remove name="FormsAuthenticationModule" /> <remove name="UrlRoutingModule-4.0" /> <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule"/> <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler"/> <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" /> <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler"/> <add name="ClearServerError" type="Suls.Web.Common.HttpModules.ClearServerErrorModule" /> </modules>

    我不明白为什么会这样,因为据我所知 MS docs preCondition .

    如果有人有好的解释,我很乐意接受他们的回答