I have an action filter checks whether or not a session variable ID
is set. For development purposes, I have manually set this variable prior to the check.
public class MyActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext context)
{
context.HttpContext.Session.Add("ID", 123123);
int ID = (int)context.HttpContext.Session.Contents["ID"];
var rd = context.HttpContext.Request.RequestContext.RouteData;
TED _db = new TED();
//if not in DB
if (_db.Users.Find(ID) == null && rd.GetRequiredString("action") != "NoAccess")
{
RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("action", "NoAccess");
redirectTargetDictionary.Add("controller", "Home");
redirectTargetDictionary.Add("area", "");
context.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
base.OnActionExecuted(context);
}
}
As far as I understand, this code is run prior to any page, this Session["ID"]
is always set. The site works fine if I am consistently testing, but it seems to break if I leave it for a while then try to continue testing. Here is the error I get:
int UserID = (int)Session.Contents["ID"];
System.NullReferenceException: Object reference not set to an instance of an object.
Initially I thought the session may simply be expiring, but prior to any page loading, Session["ID"]
should be getting set. What is the issue here?
A session variable is a special type of variable whose value is maintained across subsequent web pages. With session variables, user-specific data can be preserved from page to page delivering customized content as the user interacts with the web application.
Sessions values can be stored for the duration of the visitor's session on your site. Most cases, they are stored in server memory. You can configure session to store either in State server or in SQL Server. In ASP.NET MVC, you can create and access session variables using HttpContext.
The Session object stores information about, or change settings for a user session. Variables are stored in a Session object hold information about one single user. And are available to all pages in one application. Common information stored in session variables are name, id, and preferences.
you're implementing your actionfilter's on OnActionExecuted
method which executes AFTER your action method
You should implement the OnActionExecuting
method
public class MyActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
context.HttpContext.Session.Add("ID", 123123);
int ID = (int)context.HttpContext.Session.Contents["ID"];
var rd = context.HttpContext.Request.RequestContext.RouteData;
TED _db = new TED();
//if not in DB
if (_db.Users.Find(ID) == null && rd.GetRequiredString("action") != "NoAccess")
{
RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("action", "NoAccess");
redirectTargetDictionary.Add("controller", "Home");
redirectTargetDictionary.Add("area", "");
context.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
base.OnActionExecuting(context);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With