The aim of this article is to explain the working of action filter. It is an attribute that can be applied in the controller. It tell that which the action execution is done. Action method in the asp.net mvc

Output cache- Output Cache cache the output for given interval of time

Handle error-Handle error action attribute handle the error when error occurred by controller. It redirect to another page.

Authorize-Authorize action provide the authority for particular user or user. It provide restriction.

There are four type of filter that support asp.net mvc

1-Authorization filter It implements IAuthorization attribute. It contain the logic for authentication for controller action.

2-Action Filter- It implements  IActionFilter attribute. It contain the logic that is executed before and after control action executes.

3-Result Filter-It implements IResult attribute. It contain the logic that is executed before and after the view result execution.

4-Exeception Filter-It implents IException attribute. It contain the logic for handle error it executes at the last of all filter.

These all type of filter is executed as it is written in serial .

If we want to change the order of execution of filter then we have to use Filter order property  

Let us take a example to execute               

Outputcache

using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Demo.Models;
 
 
namespace Demo.Controllers
{
    public class HomeController : BaseController
    {         
        [OutputCache (Duration=20)]
        public ActionResult Index()
        {
            ViewBag.Users =
Uow.EmpDetail.SelectAll();      
           
            return View();
        }
}

 

This attribute hold the output for 20 seconds.

HanddleError

using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Demo.Models;
 
 
namespace Demo.Controllers
{
    [HandleError]
    public class HomeController : BaseController
    {    
     
        [OutputCache (Duration=20)]
        public ActionResult Index()
        {
            ViewBag.Users =
Uow.EmpDetail.SelectAll();      
           
            return View();
        }}
}


Authorize 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Demo.Models;
 
 
namespace Demo.Controllers
{
    [HandleError]
    public class HomeController : BaseController
    {         
        [OutputCache (Duration=20)]
        [Authorize]
        public ActionResult Index()
        {
            ViewBag.Users =
Uow.EmpDetail.SelectAll();      
           
            return View();
        }
 
    [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(EmpDetail model)
        {
            if (ModelState.IsValid)
            {
                if (model.Id == 0)
                {
                   
Uow.EmpDetail.Insert(model);
 
                }
                else
                {
                    var data =
Uow.EmpDetail.SelectByID(model.Id);
                    if (data == null)
                       
Uow.EmpDetail.Insert(model);
                    else
                       
Uow.EmpDetail.Update(model);
                }
            }
                ViewBag.Users =
Uow.EmpDetail.SelectAll();
                return PartialView("UserList");          
         
        }    
        [HttpPost]
        [Authorize(Roles="Admin")]
        public ActionResult Delete(int Id)
        {
            Uow.EmpDetail.Delete(Id);         
            ViewBag.Users =
Uow.EmpDetail.SelectAll();           
            return PartialView("UserList");
        }    
        public ActionResult Edit(int Id)
        {
            var data = Uow.EmpDetail.SelectByID(Id);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
      
 
    }
}
 


In the above example I have use authorize attribute in two place in index action and in delete action .If we try to access without log in then it shows error.

Understanding Action filter(c#)MVC

What are the different types of filters, in an asp.net mvc application

What is the use of action filters in an MVC application

  Modified On Mar-17-2018 03:04:28 AM

Leave Comment