微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 在ASP.NET MVC中获取当前操作/控制器的自定义属性列表

检查从为ASP.NET MVC2编写的 http://lukesampson.com/post/471548689/entering-and-exiting-https-with-asp-net-mvc的示例代码,我注意到他们可以通过访问filterContext.ActionDescriptor和filterContext.ActionDescriptor.ControllerDescriptor来检查自定义属性是否应用于当前操作或控制器:
public class ExitHttpsIfNotrequiredAttribute : Filterattribute,IAuthorizationFilter {
    public void OnAuthorization(AuthorizationContext filterContext) {
        // snip

        // abort if a [RequireHttps] attribute is applied to controller or action
        if(filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute),true).Length > 0) return;
        if(filterContext.ActionDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute),true).Length > 0) return;

        // snip
    }
}

什么是ASP.NET MVC 1方法检查动作和控制器的自定义属性?在ASP.NET MVC 1中没有我可以告诉的filterContext.ActionDescriptor.

解决方法

更好更可靠*方法
filterContext.ActionDescriptor.GetCustomAttributes(
    typeof(RequireHttpsAttribute),true).Count> 0

虽然这可能只是MVC 3.0.

原文地址:https://www.jb51.cc/aspnet/245880.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐