AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Thursday 14 January 2016

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.

 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.

   1 comment :

  1. Awesome site very helpful. keep posting.

    ReplyDelete

  • Popular Posts
  • Comments