相关疑难解决方法(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万
查看次数

如何知道Controller在ASP.net中是否具有属性?

在视图中,例如,在"_Layout.cshtml"中

如何获取调用此视图的控制器/操作?

找到控制器/动作名称后,如何获取它具有的属性列表?或者测试它是否有属性?

谢谢.

asp.net-mvc attributes action controller view

0
推荐指数
1
解决办法
1505
查看次数