asp.net-core – 检查动作过滤器中的属性
发布时间:2020-12-16 07:07:02 所属栏目:asp.Net 来源:网络整理
导读:在MVC 5中,您可以在IActionFilter中执行类似的操作,以检查是否已在当前操作(或控制器范围)上声明属性 public void OnActionExecuting(ActionExecutingContext filterContext){ // Stolen from System.Web.Mvc.AuthorizeAttribute var isAttributeDefined = f
在MVC 5中,您可以在IActionFilter中执行类似的操作,以检查是否已在当前操作(或控制器范围)上声明属性
public void OnActionExecuting(ActionExecutingContext filterContext) { // Stolen from System.Web.Mvc.AuthorizeAttribute var isAttributeDefined = filterContext.ActionDescriptor.IsDefined(typeof(CustomAttribute),true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(CustomAttribute),true); } 因此,如果你的控制器像这样定义属性,这是有效的. [CustomAttribute] public ActionResult Everything() { .. } 是否可以在ASP.NET Core MVC(IActionFiler内)中执行相同的操作? 解决方法
是的,你可以做到.这是ASP.NET Core的类似代码.
public void OnActionExecuting(ActionExecutingContext context) { var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor; if (controllerActionDescriptor != null) { var isDefined = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true) .Any(a => a.GetType().Equals(typeof(CustomAttribute))); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – System.Web.HttpContext.Current在检查Cache后自
- asp.net-mvc-3 – 需要帮助区分WebMatrix,ASP.NET网页和Raz
- asp.netcore di 实现批量接口注入
- 在asp.net中发布回发和浏览器后退按钮问题
- asp.net-mvc – 使用Viewbag绑定DropdownlistFor
- asp.net – DataList中的分页无法正常工作
- asp.net – 访问listview的edititemtemplate中的控件
- VS 2013 RC中缺少ASP.NET Web窗体脚手架功能
- asp.net-mvc-3 – ASP.NET MVC 3在web.config中的区域和多个
- asp.net-mvc-3 – 如何在Mvc3中填充ListBoxFor?
推荐文章
站长推荐
- asp.net-mvc – localhost拒绝在visual studio中
- asp.net-mvc – ASP.NET MVC术语让我沮丧 – 为什
- asp.net – asp mvc http以对象作为参数获取动作
- asp.net-mvc-4 – 最小和最大字符串长度的单独错
- asp.net-mvc – Json返回时如何读取modelstate错
- asp.net – 使用Microsoft Chart控件在图表上标记
- ASP.NET启动web调试,窗体自动放大的方法
- asp.net – 缓存viewstate?
- asp.net-mvc – Azure服务总线队列触发器功能在运
- asp.net-mvc – MVC4 Web Api中的[Bind(Prefix =
热点阅读