What is the equivalent of Web Forms "Page_Load" in ASP.Net Core 5, so my code will run before any page loading?

Stack Overflow Asked by on November 5, 2020

Is there a way to execute a code in every page loading in ASP.Net Core 5 like in Web Forms?
In Web Forms I used the "Page_Load", but what is the equivalent in ASP.Net Core 5?
so if I call any Action in any Controller, it will run my code first, then run the Action execution.

I found this : How can I execute common code for every request?
But when I tried it I got errors.

Please someone provide me with clear solution.

Also I need a solution to check the session from one place, instead of writing the "check session code" in each controller, I create the session after the login is succeed, but what is the best way to check the session in all controllers and if it is null then redirect to login page.

One Answer

In core, you can use Action filters to replace the "Page_Load" method in webform.

You can register the global scope of Action filters in the startup to ensure that the Action filters will be executed before each action is executed.

Add the following Action filter in your project :

public class MyActionFilter : IActionFilter
        public void OnActionExecuting(ActionExecutingContext context)

                // Do something before the action executes.
            if (!context.HttpContext.Request.Path.ToString().Contains("Login"))
                if (context.HttpContext.Session.GetString("user") == null)
                    context.Result = new RedirectToRouteResult(
                        new RouteValueDictionary { { "controller", "Login" }, { "action", "Index" } });

        public void OnActionExecuted(ActionExecutedContext context)
            // Do something after the action executes.

Then in startup.cs ConfigureServices method, add following code to apply it to all scope:

services.AddControllersWithViews(options =>

Correct answer by Yongqing Yu on November 5, 2020

Add your own answers!

Related Questions

How can I get different margins when appending divs in CSS?

4  Asked on February 4, 2021 by michaelstackquestion


Getting error while running merged jtl files

2  Asked on February 4, 2021 by ajij-shaikh


Why don’t need to use free() function in this case?

2  Asked on February 4, 2021 by akrilmokus


how to show data of databse in navigation bar on laravel 7

2  Asked on February 4, 2021 by fahad-munir


String formatting: optional section

1  Asked on February 3, 2021 by cerno


Multiple table to encode json and display

1  Asked on February 3, 2021 by j-wujeck


i need to remove duplicated comments from every post

1  Asked on February 3, 2021 by rabie_alkholi


Why is C is much slower as compared to Java?

1  Asked on February 3, 2021 by jaysmito-mukherjee


Server returned HTTP response code: 429 for URL JAVA Reddit JSON

2  Asked on February 3, 2021 by luke-prior


How to measure sequential memory read speed in C/C++

1  Asked on February 3, 2021 by sz-ppeter


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP