排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术
原
net core 过滤器。net core 过滤器获取特性。过滤器获取控制器名称,获取action名称

注册过滤器的位置和net framework有点区别,但是也差不多。
3.x版本中
services.AddControllersWithViews(a => { //配置一个ServiceFilter a.Filters.AddService<CheckTokenFilter>(); }).AddRazorRuntimeCompilation();
在过滤器中获取action与controller的名称
转成ControllerActionDescriptor就可以了
public void OnActionExecuting(ActionExecutingContext context) { if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor) { string actionName = controllerActionDescriptor.ActionName; string controllerName = controllerActionDescriptor.ControllerName; if (controllerName.ToLower() == "User".ToLower() && actionName.ToLower() == "ArticleDetials".ToLower()) { } } }
下面说一下在过滤器中常用的几个小知识
获取控制器,action的特性
获取当前所有的过滤器
跳转页面
public override void OnActionExecuting(ActionExecutingContext filterContext) { var noNeedCheck = false; if (filterContext.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor) { //noNeedCheck = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true) // .Any(a => a.GetType().Equals(typeof(NoSignAttribute))); //获取请求controller的特性 object[] noSignAttribute = controllerActionDescriptor.ControllerTypeInfo.GetCustomAttributes(typeof(NoSignAttribute), false); noNeedCheck = noSignAttribute.Any(a => a.GetType().Equals(typeof(NoSignAttribute))); } if (noNeedCheck) return; //获取当前所有的过滤器 var filter = filterContext.Filters; //跳转页面 filterContext.Result = new RedirectResult("/login/index"); }
上面有获取控制器上面的NoSignAttribute,是自定义的。可以使用验证一些东西。比如有这个特性就不进行过滤器的逻辑验证了
public class NoSignAttribute : Attribute { }
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)
评价