MVC SessionStateAttribute does not work as a global attribute

How to configure SessionStateAttribute as a global filter in MVC3? In my Global.asax, I have this in the RegisterGlobalFilters method.

filters.Add(new SessionStateAttribute(SessionStateBehavior.Disabled)); 

And in my home controller I have this.

 public class HomeController : Controller { public ActionResult Index() { ViewBag.Message = "Welcome to ASP.NET MVC!"; Session["Blend"] = "Will it blend?"; return View(); } public ActionResult About() { return View(); } } 

But for some reason, it still allows me to use the session. However, if I decorate the HomeController class with an attribute myself, I get an error in a line using Session, where the object reference is null, what do I assume if the session is never created?

I begin to doubt that something is wrong with my project. I get small issues like this with standard behavior that should work.

Has anyone had problems with such things?

+6
session attributes asp.net-mvc-3 session-state
source share
1 answer

SessionStateAttribute not an action filter , so you cannot add it as a global action filter. This is a special attribute that allows you to decorate your controllers and have finer control over the session mode on the controller.

To disconnect a session globally for the entire application, put the following in your web.config file:

 <sessionState mode="Off" /> 
+11
source share

All Articles