49

In WebForm we could write a method in MasterPage.cs and it ran in each request .
e.g:

MasterPage.cs
--------------
protected void Page_Load(object sender, EventArgs e)
{
   CheckCookie();
}

How can we do something like this in MVC ?

5
  • 1
    Maybe this stackoverflow.com/questions/2650269/…
    – V4Vendetta
    Commented Mar 1, 2012 at 6:15
  • 1
    You want to put an ActionFilterAttribute on the controller Commented Mar 1, 2012 at 6:17
  • I would also look at what the method is doing and if it is still needed in MVC, no point porting something that new technology makes obsolete.
    – TheRealTy
    Commented Mar 1, 2012 at 6:17
  • @tyrongower: CheckCookie() must check client cookies and it can be access to session ! Commented Mar 1, 2012 at 6:19
  • I built an ASP.NET Core 2 app, using Razor Pages, Razor Pages, don't a have controller?
    – 001
    Commented Jan 25, 2018 at 3:41

2 Answers 2

104

In ASP.NET MVC you could write a custom global action filter.


UPDATE:

As requested in the comments section here's an example of how such filter might look like:

public class MyActionFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var fooCookie = filterContext.HttpContext.Request.Cookies["foo"];
        // TODO: do something with the foo cookie
    }
}

If you want to perform authorization based on the value of the cookie, it would be more correct to implement the IAuthorizationFilter interface:

public class MyActionFilterAttribute : FilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        var fooCookie = filterContext.HttpContext.Request.Cookies["foo"];

        if (fooCookie == null || fooCookie.Value != "foo bar")
        {
            filterContext.Result = new HttpUnauthorizedResult();
        }
    }
}

If you want this action filter to run on each request for each controller action you could register it as a global action filter in your global.asax in the RegisterGlobalFilters method:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new MyActionFilterAttribute());
}

And if you need this to execute only for particular actions or controllers simply decorate them with this attribute:

[MyActionFilter]
public ActionResult SomeAction()
{
    ...
}
6
  • Could you please write a sample for it? Commented Mar 6, 2012 at 7:53
  • @Mohammad, sure, I thought you read the article that I have linked to in my answer and tried to implement the sample code shown there. Apparently I was wrong in thinking this. So I have updated my answer to show an example. Commented Mar 6, 2012 at 8:21
  • Thanks dude, but a question. how can we redirect to an action in MyActionFilterAttribute? Commented Mar 10, 2012 at 7:25
  • @Mohammad, by assigning an instance of RedirectToRouteResult to the filterContext.Result property in your action filter. Commented Mar 10, 2012 at 7:27
  • @DarinDimitrov: Thanks for very good answer. But how about if I create a class that inherit from controller and check that code in it's constructor and inherit all controller from this new class? Is this a good idea? Thanks
    – DooDoo
    Commented Jan 16, 2018 at 19:35
8

You could use Global.asax Application_AcquireRequestState method which will get called on every request:

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
     //...
}
2
  • 2
    So, can we access to Cookie and Session from above method ? Commented Mar 6, 2012 at 7:14
  • 2
    Yes you can. but this method may be execute more than one in per request Commented Nov 13, 2016 at 11:13

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