How can I disable session state in ASP.NET MVC?

asp.net MvcSession StateTempdata

asp.net Mvc Problem Overview


I would like to have a very lightweight ASP.NET MVC site which includes removing as many of the usual HttpModules as possible and disabling session state. However when I try to do this, I get the following error:

The SessionStateTempDataProvider requires SessionState to be enabled.

I've disabled session state in web.config:

<sessionState mode="Off" />

I understand that ASP.NET MVC uses session state for TempData, but I don't need/want TempData - I just want to disable session state. Help!

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

You could make your own ControllerFactory and DummyTempDataProvider. Something like this:

public class NoSessionControllerFactory : DefaultControllerFactory
{
  protected override IController GetControllerInstance(Type controllerType)
  {
    var controller = base.GetControllerInstance(controllerType);
    ((Controller) controller).TempDataProvider = new DummyTempDataProvider();
    return controller;
  }
}


public class DummyTempDataProvider : ITempDataProvider
{
  public IDictionary<string, object> LoadTempData(ControllerContext controllerContext)
  {
    return new Dictionary<string, object>();
  }
  
  public void SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values)
  {
  }
}

And then you would just need to register the controller factory on app startup - e.g. you could do this in global.asax:

ControllerBuilder.Current.SetControllerFactory(new NoSessionControllerFactory());

Solution 2 - asp.net Mvc

I've found one way, which I don't particularly care for:

Create NoTempDataProvider

using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Facebook.Sites.Desktop.Auth.Models
{
    public class NoTempDataProvider : ITempDataProvider
    {
        #region [ ITempDataProvider Members ]

        public IDictionary<String, Object> LoadTempData(ControllerContext controllerContext)
        {
            return new Dictionary<String, Object>();
        }

        public void SaveTempData(ControllerContext controllerContext, IDictionary<String, Object> values) { }

        #endregion
    }
}

Manually Overwrite the Provider in the Controller

public class AuthController : Controller
{
    public AuthController()
    {
        this.TempDataProvider = new NoTempDataProvider();
    }
}

I would greatly prefer a way to do this completely via the configuration, but this works for now.

Solution 3 - asp.net Mvc

If you need to use TempData for simple strings, you can use the CookieTempDataProvider in MvcFutures http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471.

Solution 4 - asp.net Mvc

According to Brad Wilson, this has been fixed in MVC 2 Preview 1. See here and here.

Solution 5 - asp.net Mvc

Modern solution: > In ASP.NET, if you do not use the Session object to store any data or > if any of the Session events (Session_OnStart or Session_OnEnd) is > handled, session state is disabled.

So not using Session (or TempData), disables Session State.

Source

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel SchafferView Question on Stackoverflow
Solution 1 - asp.net MvcSteve WillcockView Answer on Stackoverflow
Solution 2 - asp.net MvcDaniel SchafferView Answer on Stackoverflow
Solution 3 - asp.net MvcHaackedView Answer on Stackoverflow
Solution 4 - asp.net MvcRaleigh BucknerView Answer on Stackoverflow
Solution 5 - asp.net MvcErTRView Answer on Stackoverflow