Filters in ASP.NET MVC

Keywords: ASP.NET Attribute

Here, examples are given to illustrate the usage of various filters. If there are some mistakes, please ask God to point out and discuss them together.

1. ActionFilter method filter:

The interface is called IActionFilter, which executes before/after the controller method call.

In the new MVC program, add a class MyFilter1Attribute and inherit the ActionFilterAttribute abstract class

As you can see from the figure above, all the methods in ActionFilterAttribute are introduced. We can implement custom Filter by inheriting the ActionFilterAttribute class and override its methods.

   public class MyFilter1Attribute: ActionFilterAttribute
    {
        /// <summary>
        /// The method will be Action Call before method execution
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Write("I am OnActionExecuting,I am here Ation Execution before method invocation<br/>");
            base.OnActionExecuting(filterContext);
        }


        /// <summary>
        /// The method will be Action Method invocation after execution
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            filterContext.HttpContext.Response.Write("I am OnActionExecuted,I am here Action Execution after method invocation<br/>");
            base.OnActionExecuted(filterContext);
        }

    }

Then create a HomeController controller and add the Test Action of FilterTest

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [MyFilter1]
        public void FilterTest()
        {
            Response.Write("I am Action Method, I executed it here......<br/>");
        }
    }

Run the program and access the FilterTest method:

The figure above shows a sequence of execution.

Sometimes there may be scenarios where we need to jump out and not perform subsequent methods when an Action identifies an Attribute. We can use the IsDefained method in the ActionDescription class in the filterContext to make a judgment check.

 

     /// <summary>
        /// The method will be Action Call before method execution
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Write("I am OnActionExecuting,I am here Ation Execution before method invocation<br/>");
            //judge Action Whether the method is affixed or not MyFilter1Attribute Label
            if (filterContext.ActionDescriptor.IsDefined(typeof(MyFilter1Attribute), false))
            {
                //If so, why not? Action Method direct return ContentResult,Then the Action The method has a return value here, which is equivalent to the method that ends here and will not be executed again, for example: OnActionExecuted
                filterContext.Result = new ContentResult();
            }
            base.OnActionExecuting(filterContext);
        }

 

2.ResultFilter result filter:

The interface is called IResultFilter, which is called before or after the controller method is invoked and jumps to the View page.

 

 

 

3.ExceptionFilter exception operation filter:

The interface is called IExceptionFilter and executes when the controller's Action method throws an exception

 

4. Authorization Filter Authorization Filter Authorization Filter:

The interface is called IauthorizationFilter, which is executed first of all filters

Posted by rscott7706 on Mon, 30 Sep 2019 09:32:42 -0700