Run a method before each Action in ASP.NET MVC
In this blog post we will discuss on running a method before each action in ASP.NET MVC. If you want to check some condition before the action execution, then it helpful for you. Please read below how doing it?
Step 1: Add new BaseController Class in your Project & Inherit it from Controller class.
Step 2: Inherit other controllers from BaseController.
If we call "YourAction" action method then BaseController "OnActionExecuting" method will be checked first before execution. Same process will occur with any action in this "YouController" controller.
Step 1: Add new BaseController Class in your Project & Inherit it from Controller class.
public class BaseController : Controller { protected override void OnActionExecuting(ActionExecutingContext filterContext) { //Write you code logic that executes before action } }
Step 2: Inherit other controllers from BaseController.
public class YourController : BaseController { public ActionResult YourAction() { return View(); } }
If we call "YourAction" action method then BaseController "OnActionExecuting" method will be checked first before execution. Same process will occur with any action in this "YouController" controller.
Awesome site very helpful. keep posting.
ReplyDelete