Interceptor in ASP.NET MVC

There are three hooks in ASP.NET MVC:Representionic interceptorInterceptor resultAndException interceptorSo the interceptor is nothing, just write one class, inherit another class and one interface, and access a method in the interface! Let’s implement them one by one!

public class ExceptionFillters : FilterAttribute,IExceptionFilter
    {
        // выполнить этот код, когда возникают аномалии
        public void OnException(ExceptionContext filterContext)
        {
                         // Здесь вы можете записать, что вы должны делать, когда произошло ненормальное, напишите журнал в пропорции

                         // Эта строка сообщает системе, что это исключение было рассмотрено, не нужно иметь дело с
            filterContext.ExceptionHandled = true;
        }
    }
    public class ActionFillters : FilterAttribute, IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
                         // После выполнения действия выполните этот метод, например, выполнение операционного журнала
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
                       // выполнить этот метод перед выполнением действия, такого как проверка личности
        }
    }
    public class ResultFillters : FilterAttribute, IResultFilter
    {
        public void OnResultExecuted(ResultExecutedContext filterContext)
        {
                       // после выполнения действия выполните после прыжка
        }

        public void OnResultExecuting(ResultExecutingContext filterContext)
        {
                         // выполнить его перед прыжком после выполнения действия
        }
    }

  When you use it, it is more convenient. It’s really easy to use properties in C#. just add a label;

1 [ResultFillters]
2 [ActionFillters]
3 [ExceptionFillters]
4 public ActionResult Index()
5 {
6     ViewData["Message"] = "Welcome to ASP.NET MVC!";
7     return View();
8 }
9     
	

Reprinted from: https://www.cnblogs.com/yhongl/archive/2012/07/24/2605822.html

Leave a Comment