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 ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
If you want to perform authorization based on the value of the cookie, it would be more correct to implement the IAuthorizationFilter interface:
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
RegisterGlobalFiltersmethod:And if you need this to execute only for particular actions or controllers simply decorate them with this attribute: