asp.net? Use filter filter to intercept

ActionFilterAttribute

FilterConfig can add filters, such as adding an ActionFilterAttribute. The operation of each page will pass the filter, which can be used in some scenarios, such as login token verification. If there is no token or the token fails, you can directly redirect to the login page. Here is a simple example.
Customize an ActionFilterAttribute:

namespace web.App_Start
{
 
    public class HandleTestAttribute: System.Web.Mvc.ActionFilterAttribute
    {
      
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            var controllerName = filterContext.RouteData.Values["controller"].ToString();
            var actionName = filterContext.RouteData.Values["action"].ToString();

            filterContext.HttpContext.Response.Write("End time:" + DateTime.Now.ToString() + "<br/>");
            filterContext.HttpContext.Response.Write("controller:" + controllerName + ",action:" + actionName);
        }
    }
}

Add the HandleTestAttribute to FilterConfig:

namespace web
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
           
            filters.Add(new HandleErrorAttribute());
            filters.Add(new HandleTestAttribute());
        
        }
    }


}

You can see from the operation that after each page operation, the text written out by the filter is written out:

Request filtering and global web api request monitoring

Sometimes all requests are filtered, such as adding a global request listener and logging.
You need to add your custom interceptor in WebApiConfig:

 public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure the Web API to use only anonymous token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

            // Web API routing
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            config.Filters.Add(new RequestHandler());
        }
    }

RequestHandler:

namespace web.App_Start
{
    public class RequestHandler: ActionFilterAttribute
    {
        public static readonly ILog loginfo = LogManager.GetLogger("loginfo");
       
        public override void OnActionExecuting(HttpActionContext filterContext)
        {
           
            base.OnActionExecuting(filterContext);         
            loginfo.Info("I got the request content:");

        }


    }
}

In this way, every api request received can be logged

Posted by pfdesigns on Sat, 16 Nov 2019 07:17:03 -0800