Search code examples
asp.net-mvcasp.net-mvc-3httpcontextaction-filter

Questions regarding HttpContext, HttpContextBase, and Action Filters


I'm try to build a static property on a static class that will basically return a cookie value, to be used across my MVC site (MVC 3, if it matters). Something like this:

public static class SharedData
{
    public static string SomeValue
    {
        get
        {
            if (HttpContext.Current.Request.Cookies["SomeValue"] == null)
            {
                CreateNewSomeValue();
            }

            return HttpContext.Current.Request.Cookies["SomeValue"].Value.ToString();
        }
    }
}

I need to access this from within controller actions, global.asax methods, and action filters. But the problem is, when action filters run, HttpContext is not available. Right now, I have to have a separate static method just to pull the cookie from the filter context that I pass in, which seems awkward.

What is the best solution for building such a static method for retrieving a cookie value like this that works from both controller actions and action filters? Or is there a better approach for doing something like this?

Thanks in advance.


Solution

  • As JohnnyO pointed out above, I had access to HttpContext from within my action filter all along. At least, in the particular action filter method where this was needed. There may have been some other filter/method that did not have access at one point, but for now, this is working as I need it to.