相关疑难解决方法(0)

如何在ASP.NET Core MVC中读取操作方法的属性?

基于这篇文章,我正在尝试IActionFilter为ASP.NET Core 创建一个实现,它可以处理控制器上标记的属性和控制器的操作.虽然读取控制器的属性很容易,但我无法找到一种方法来读取action方法中定义的属性.

这是我现在的代码:

public sealed class ActionFilterDispatcher : IActionFilter
{
    private readonly Func<Type, IEnumerable> container;

    public ActionFilterDispatcher(Func<Type, IEnumerable> container)
    {
        this.container = container;
    }

    public void OnActionExecuting(ActionExecutingContext context)
    {
        var attributes = context.Controller.GetType().GetCustomAttributes(true);

        attributes = attributes.Append(/* how to read attributes from action method? */);

        foreach (var attribute in attributes)
        {
            Type filterType = typeof(IActionFilter<>).MakeGenericType(attribute.GetType());
            IEnumerable filters = this.container.Invoke(filterType);

            foreach (dynamic actionFilter in filters)
            {
                actionFilter.OnActionExecuting((dynamic)attribute, context);
            }
        }
    }

    public void OnActionExecuted(ActionExecutedContext context)
    {
        throw …
Run Code Online (Sandbox Code Playgroud)

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

48
推荐指数
4
解决办法
2万
查看次数

标签 统计

.net ×1

asp.net-core ×1

asp.net-core-mvc ×1

c# ×1