ASP.NET MVC-处理404:SessionStateTempDataProvider需要启用SessionState [英] ASP.NET MVC - Handling 404's: The SessionStateTempDataProvider requires SessionState to be enabled

查看:174
本文介绍了ASP.NET MVC-处理404:SessionStateTempDataProvider需要启用SessionState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有此处所述的安装错误处理:

I have setup error handling as described here: How can I properly handle 404 in ASP.NET MVC?

当调用errorController.Execute方法时,我收到一个InvalidOperationException:SessionStateTempDataProvider需要启用SessionState.

When the errorController.Execute method is called, I get an InvalidOperationException: The SessionStateTempDataProvider requires SessionState to be enabled.

我的会话状态模式设置为InProc,但是我没有使用它,所以我也尝试按照以下说明将其关闭:

My session state mode is set to InProc, but I'm not using it so I also tried turning it off as described here: How can I disable session state in ASP.NET MVC? The code is executed, but I still get the error.

这是使用Visual Studio内置的Web浏览器在本地进行的.

This is happening locally using the Visual Studio built-in web browser.

有没有办法解决这个问题?

Is there a way to fix this?

推荐答案

可以通过在ErrorController中重写ExecuteCore方法来解决此问题.显然,某些类型的错误(例如,禁止访问文件)不会完全填充错误处理程序可用的HttpContext;特别是Context.Session == null,这会导致ExecuteCore方法阻塞,试图确定是否需要保存/加载任何TempData.

This problem can be fixed by overriding the ExecuteCore method in your ErrorController. Apparently some kinds of errors (e.g. forbidden file access) don't fully populate the HttpContext that's available to the error handler; in particular Context.Session == null, which causes the ExecuteCore method to choke trying to determine if there's any TempData that needs to be saved/loaded.

我决定我可以在错误控制器中不使用TempData.这是我的实现.

I decided I can live without TempData in my error controller; here is my implementation.

public class ErrorController : Controller {
  protected override void ExecuteCore() {
    string actionName = RouteData.GetRequiredString("action");
    if (!ActionInvoker.InvokeAction(ControllerContext, actionName)) {
      HandleUnknownAction(actionName);
    }
  }

  [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
  public ViewResult InternalServerError() {
    Response.StatusCode = (int)HttpStatusCode.InternalServerError; // 500
    return View();
  }

  [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
  public ViewResult NotFound(string Path) {
    Response.StatusCode = (int)HttpStatusCode.NotFound; // 404
    ViewData["Path"] = Path;
    return View();
  }
}

这篇关于ASP.NET MVC-处理404:SessionStateTempDataProvider需要启用SessionState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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