过滤器是什么
简单理解就和净水器一个原理。请求进来之后过滤请求用的。
过滤器的应用
这里的发现一个很有意思的地方,.net Core2.1的过滤器和.net Core 3.1 的过滤器使用方法是有点区别的,.net Core 3.1的使用方法更优雅了。
在.net Core 中过滤器的允许顺序是:授权过滤器->资源过滤器->操作过滤器->异常过滤器->结果过滤器
这里是.Net Core 2.1里的写法
在.net Core 3.1里filterContext.HttpContext.Response.Writer()这个方法没有了
public class MyActionFilterAttribute:ActionFilterAttribute
{
//请求过滤
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//获取此次请求的控制器
string strController = filterContext.RouteData.Values["controller"].ToString();
//获取此次请求的方法名称
string strAction = filterContext.RouteData.Values["action"].ToString();
//请求id
string Id = filterContext.ActionDescriptor.Id.ToString();
filterContext.HttpContext.Response.Writer("控制器名称" + strController);
filterContext.HttpContext.Response.Writer("方法名称" +strAction);
filterContext.HttpContext.Response.Writer("请求id" + Id);
filterContext.HttpContext.Response.Writer("请求时间开始" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.Writer("请求返回时间"+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
base.OnActionExecuted(filterContext);
}
这是.net Core 3.1里的写法
1.方法一:还是可以根据2.1中的使用方法去用。但是不够优雅,这里是重写的父类的纯虚函数
public class MyActionFilterAttribute:ActionFilterAttribute
{
//请求过滤
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//获取此次请求的控制器
string strController = filterContext.RouteData.Values["controller"].ToString();
//获取此次请求的方法名称
string strAction = filterContext.RouteData.Values["action"].ToString();
//请求id
string Id = filterContext.ActionDescriptor.Id.ToString();
filterContext.HttpContext.Response.Headers.Add("控制器名称" , strController);
filterContext.HttpContext.Response.Headers.Add("方法名称" , strAction);
filterContext.HttpContext.Response.Headers.Add("请求id" , Id);
filterContext.HttpContext.Response.Headers.Add("请求时间开始" , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.Headers.Add("请求返回时间", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
base.OnActionExecuted(filterContext);
}
}
2.方法二:实现微软给我们提供的接口 IActionFilter,效果其实差不多,只是少去了调用父类方法那一步
public class MyActionFilterAttribute: IActionFilter
{
//请求过滤
public void OnActionExecuting(ActionExecutingContext filterContext)
{
//获取此次请求的控制器
string strController = filterContext.RouteData.Values["controller"].ToString();
//获取此次请求的方法名称
string strAction = filterContext.RouteData.Values["action"].ToString();
//请求id
string Id = filterContext.ActionDescriptor.Id.ToString();
filterContext.HttpContext.Response.Headers.Add("控制器名称" , strController);
filterContext.HttpContext.Response.Headers.Add("方法名称" , strAction);
filterContext.HttpContext.Response.Headers.Add("请求id" , Id);
filterContext.HttpContext.Response.Headers.Add("请求时间开始" , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.Headers.Add("请求返回时间", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
}