排名
1
文章
860
粉丝
112
评论
163
.net core自定义项目模板,创建自己的模板项目,使用命令行创建模板项目
尘叶心繁 : 可以可以讲真的我都想弄个模板
net core webapi post传递参数
庸人 :
确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
.net webapi 返回需要的字段,忽略某些字段,修改字段名等
雨雨雨雨雨辰 : 已精
.net webapi 返回需要的字段,忽略某些字段,修改字段名等
雨雨雨雨雨辰 :
疯狂反射
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
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)
评价