2

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

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

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.

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

Is there a way to fix this?

Community
  • 1
  • 1
Josh Close
  • 22,935
  • 13
  • 92
  • 140

2 Answers2

1

If you want to leave session state on, try adding the following to <system.webServer> <modules>:

<add name="SessionStateModule" type="System.Web.SessionState.SessionStateModule" />
anonymous
  • 51
  • 1
  • This doesn't get rid of the Error for me and I am also using the Visual Studio built-in Cassini server. – OutOFTouch Jan 14 '13 at 16:30
  • This error only occurs if using built in web-dev and IE, it does not occur when using the built-in web dev and FF and does not occur when using IE and IIS 7, strange. – OutOFTouch Jan 14 '13 at 21:04
0

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.

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();
  }
}
keithm
  • 2,813
  • 3
  • 31
  • 38