Introduction:

Here we will explain to what is action filter in mvc asp.net how many type filters are available in mvc. How to use action filters in mvc , what is different  action filterAuthorization FilterResult FilterException filter in mvc asp.net with example.

Description:

Action filter help you to preform logic while action method is executing and after action method executed. Some time we need to write pre and post processing logic from action method. for achieving this functionality ASP.NET MVC provides a feature called Filters.

types of filters:

  1. Action Filters
  2. Authorization Filters
  3. Result Filters
  4. Exception Filters

Action Filters:

When we need to write logic  before executing and after executed controller action that time implement Action Filter.

Example:

namespace MvcApplication1.Controllers

{

    [Log]

    public class HomeController : Controller

    {      

        public ActionResult Index()

        {

            return View();

        }   

    }

}

public class Log : ActionFilterAttribute

{

 public override void OnActionExecuted(ActionExecutedContext filterContext)

    {

        CrteateLog("OnActionExecuted", filterContext.RouteData);

    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)

    {

        CrteateLog("OnActionExecuting", filterContext.RouteData);

    }

}

Authorization Filters:

Authorization Filters are used to implement authorization and authentication for controller actions.


namespace MvcApplication1.Controllers

  {

    public class HomeController : Controller

    {

        [Authorize]

        public ActionResult Index()

        {

            return View();

        }

    }

  }


Result Filters:

 

Result filters contain logic that is executed before and after a view result is executed. For example, you want to modify  view before the view is rendered to the browser.


namespace MvcApplication1.Controllers

{

    [Log]

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            return View();

        }

    }

}

public class Log : ActionFilterAttribute

{

    public override void OnResultExecuted(ResultExecutedContext filterContext)

    {

        CrteateLog("OnResultExecuted",filterContext.RouteData);

    }

    public override void OnResultExecuting(ResultExecutingContext filterContext)

    {

        CrteateLog("OnResultExecuting ", filterContext.RouteData);

    }

}

Exception Filters:


 Exception filters are used to handle errors raised by either your controller actions or controller action results. You also can use exception filters to log errors.


namespace MvcApplication1.Controllers

{

    [HandleError]

    public class HomeController : Controller

    {

        [Authorize]

        public ActionResult Index()

        {

            return View();

        } 

    }

}

So above these are the action filters, I hope helpful for you.


0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

Welcome to SQL Tutorials Point

Don't have an account? Click here to Create a new account, it takes less than a minute.