0

I am trying to set a session inside a abstract class which is already written. What I am trying to do is; First I try to check the method type either is get or post. If it is GET method then set the session.

here is the code:

public abstract class BaseAbstractController : Controller
{
    public BaseAbstractController()
    {
        if (this.HttpContext.Request.HttpMethod.ToString() == "GET")
        {
            this.HttpContext.Session["testsession"] = this.HttpContext.Request.Url.AbsolutePath;
        }
    }
}

The problem I am facing is, I get the Null exception error and it is because HTTPContext value is null.

Right now there is only one MVC controller that extends from abstract controller.

4
  • 2
    When and from where is it called? Feb 12, 2015 at 12:04
  • Nothing call this method. Another controller just extends from it.
    – Capri82
    Feb 12, 2015 at 12:06
  • Yes, and that controller's contructor calls it's base constructor. But when is that controller initialized and from where? Having said that, i'm not that familiar with MVC. But obviously at that point there is no current HttpContext or HttpContext.Request. Use the debugger, set a breakpoint in the first line of the constructor. Feb 12, 2015 at 12:08
  • My guess is, the MVC or some dependency injector is doing it. May be I need to override execute/initialize function to make the httpcontext work?
    – Capri82
    Feb 12, 2015 at 12:23

2 Answers 2

2

It is better not doing that in constructor, as constructor should only construct the instance if possible. You can override OnActionExecuting instead.

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting(filterContext);
    var req = filterContext.RequestContext.HttpContext.Request;
    if (req.HttpMethod == "GET")
    {
        filterContext.RequestContext.HttpContext.Session["testsession"] = req.Url;
    }
}
0
1

Your problem is that the HttpContext property within Controller that you're referencing isn't set until AFTER the controller has been instansiated - meaning it will not be available within your abstract classes' constructor.

If you need to access the HttpContext object then your best bet is to reference the static instance directly like so: System.Web.HttpContext

public abstract class BaseAbstractController : Controller
{
    public BaseAbstractController()
    {
        if (System.Web.HttpContext.Current.Request.HttpMethod.ToString() == "GET")
        {
            System.Web.HttpContext.Current.Session["testsession"] = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        }
    }
}

Update:

To respond to your comment, if you're trying to access the HttpContext within OnActionExecuted then you should access it via the request context within the ActionExecutedContext argument like this:

protected virtual void OnActionExecuted(ActionExecutedContext filterContext)
{
    var context = filterContext.HttpContext;
}

The difference is that at this point the controller has been instantiated and the base controller's HttpContext property has been set. Ultimately all the .NET MVC framework is doing is referencing the System.Web.HttpContext static instance from Controller.HttpContext.

2
  • Thanks for the code. But what if I override Execute or some other function like OnActionExecuted of CONTROLLER inside the abstract controller and then use the HTTPcontext and setting of session?
    – Capri82
    Feb 12, 2015 at 12:37
  • HttpContext.Current is not available in OWIN self-host, so it is better to avoid using it.
    – tia
    Feb 12, 2015 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.