ASP.NET Core定义接口过滤器

1、添加接口过滤器类ActionFilter-继承IActionFilter-显示修补程序,得到如下代码

using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CommonUtils.Test.Web.Models
{
    public class ActionFilter : IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext context)
        {
            throw new NotImplementedException();
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            throw new NotImplementedException();
        }
    }
}

2、加注释:

using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CommonUtils.Test.Web.Models
{
    /// 
    /// 接口过滤器
    /// 
    public class ActionFilter : IActionFilter
    {
        /// 
        /// 开始
        /// 
        public void OnActionExecuting(ActionExecutingContext context)
        {
            //context.ActionDescriptor;
            //context.HttpContext.Request;
            Console.WriteLine("OnActionExecuting");
        }

        /// 
        /// 结束
        /// 
        public void OnActionExecuted(ActionExecutedContext context)
        {
            //context.Result;
            //context.HttpContext.Response;
            Console.WriteLine("OnActionExecuted");
        }
    }
}

3、在Startup.ConfigureServices中注入

services.AddMvc(options => options.Filters.Add());

大功告成

ASP.NET Core定义接口过滤器_第1张图片

你可能感兴趣的:(成长的程序员)