Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 238
Daniel Schaffer Avatar asked May 19 '09 20:05

Daniel Schaffer


People also ask

Are sessions enabled by default in ASP.NET MVC?

By default, Asp.Net MVC support session state. Session is used to store data values across requests. Whether you store some data values with in the session or not Asp.Net MVC must manage the session state for all the controllers in your application that is time consuming.


1 Answers

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()); 
like image 73
Steve Willcock Avatar answered Oct 06 '22 10:10

Steve Willcock