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 ?

tereško
  • 58,060
  • 25
  • 98
  • 150
Mohammad Dayyan
  • 21,578
  • 41
  • 164
  • 232

2 Answers2

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()
{
    ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Could you please write a sample for it? – Mohammad Dayyan Mar 06 '12 at 07: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. – Darin Dimitrov Mar 06 '12 at 08:21
  • Thanks dude, but a question. how can we redirect to an action in `MyActionFilterAttribute`? – Mohammad Dayyan Mar 10 '12 at 07:25
  • @Mohammad, by assigning an instance of `RedirectToRouteResult` to the `filterContext.Result` property in your action filter. – Darin Dimitrov Mar 10 '12 at 07: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 – Arian Jan 16 '18 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)
{
     //...
}
Liam
  • 27,717
  • 28
  • 128
  • 190
ionden
  • 12,536
  • 1
  • 45
  • 37