ASP.NET Core 获取控制器上的自定义属性

/// <summary>
    /// 参考代码Microsoft.AspNetCore.Mvc.Internal.AuthorizationApplicationModelProvider
    /// </summary>
    public class TestApplicationModelProvider : IApplicationModelProvider
    {
        public void OnProvidersExecuting(ApplicationModelProviderContext context)
        {
            //获取控制器上指定类型的属性
            var ad = context.Result.Controllers.First().Attributes.OfType<IAuthorizeData>();
        }

        public void OnProvidersExecuted(ApplicationModelProviderContext context)
        {
        }

        public int Order { get; } = -1000 + 10;
    }

 注册:services.AddTransient<IApplicationModelProvider, TestApplicationModelProvider>();

posted on 2016-11-05 14:58  Frank.Cui  阅读(926)  评论(0编辑  收藏  举报