小编Sta*_*855的帖子

如何在asp.net core rc2中获取控制器的自定义属性

我创建了一个自定义属性:

[AttributeUsage(AttributeTargets.Method| AttributeTargets.Class)]
public class ActionAttribute : ActionFilterAttribute
{
    public int Id { get; set; }
    public string Work { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的控制器:

[Area("Administrator")]
[Action(Id = 100, Work = "Test")]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的代码:我使用反射来查找当前程序集中的所有控制器

 Assembly.GetEntryAssembly()
         .GetTypes()
         .AsEnumerable()
         .Where(type => typeof(Controller).IsAssignableFrom(type))
         .ToList()
         .ForEach(d =>
         {
             // how to get ActionAttribute ?
         });
Run Code Online (Sandbox Code Playgroud)

有可能以ActionAttribute实用的方式阅读所有内容吗?

c# asp.net asp.net-core-mvc asp.net-core

9
推荐指数
1
解决办法
7762
查看次数

标签 统计

asp.net ×1

asp.net-core ×1

asp.net-core-mvc ×1

c# ×1