Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7576085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:50:18+00:00 2026-05-30T16:50:18+00:00

In WebForm we could write a method in MasterPage.cs and it ran in each

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:50:20+00:00Added an answer on May 30, 2026 at 4:50 pm

    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()
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a standalone webform (no masterpage) that opens in a div from another
I was wondering if Razor views could be compiled, like WebForm based views? Does
Sys.Webforms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for
With ASP.NET Webforms, I could drag and drop a control on a form and
I have a simple webform that will allow unauthenticated users to input their information,
I have the following webform: <%@ Page Language=C# AutoEventWireup=true CodeBehind=Default.aspx.cs Inherits=TestWebApp.Default %> <!DOCTYPE html
I have a .NET webform that has a file upload control that is tied
Coming from ASP.NET Webform and Classic ASP. I want to know what are the
I have a webform and i want to detect if F5 button was pressed
I have a webform with quite a few fields (between 15 to 40, based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.