1

我可以获得一个 ActionContext 或 ActionDescriptor 或可以根据路由名称描述特定操作的东西吗?

具有以下控制器。

public class Ctrl : ControllerBase
{
    [HttpGet]
    public ActionResult Get() { ... }

    [HttpGet("{id}", Name = "GetUser")]
    public ActionResult Get(int id) { ... }
}

我想做的是在调用“Get”时,能够访问“Ge​​tUser”元数据,如动词、路由参数等

就像是

ActionContext/Description/Metadata info = somerService.Get(routeName : "GetUser")

或者

ActionContext/Description/Metadata info = somerService["GetUser"];

这个想法中的一些东西。

4

2 回答 2

1

有一个 nuget 包AspNetCore.RouteAnalyzer可以提供您想要的。它公开了 HTTP 动词、mvc 区域、路径和调用的字符串。

在内部,它使用ActionDescriptorCollectionProvider来获取该信息:

           List<RouteInformation> ret = new List<RouteInformation>();

            var routes = m_actionDescriptorCollectionProvider.ActionDescriptors.Items;
            foreach (ActionDescriptor _e in routes)
            {
                RouteInformation info = new RouteInformation();

                // Area
                if (_e.RouteValues.ContainsKey("area"))
                {
                    info.Area = _e.RouteValues["area"];
                }

                // Path and Invocation of Razor Pages
                if (_e is PageActionDescriptor)
                {
                    var e = _e as PageActionDescriptor;
                    info.Path = e.ViewEnginePath;
                    info.Invocation = e.RelativePath;
                }

                // Path of Route Attribute
                if (_e.AttributeRouteInfo != null)
                {
                    var e = _e;
                    info.Path = $"/{e.AttributeRouteInfo.Template}";
                }

                // Path and Invocation of Controller/Action
                if (_e is ControllerActionDescriptor)
                {
                    var e = _e as ControllerActionDescriptor;
                    if (info.Path == "")
                    {
                        info.Path = $"/{e.ControllerName}/{e.ActionName}";
                    }
                    info.Invocation = $"{e.ControllerName}Controller.{e.ActionName}";
                }

                // Extract HTTP Verb
                if (_e.ActionConstraints != null && _e.ActionConstraints.Select(t => t.GetType()).Contains(typeof(HttpMethodActionConstraint)))
                {
                    HttpMethodActionConstraint httpMethodAction = 
                        _e.ActionConstraints.FirstOrDefault(a => a.GetType() == typeof(HttpMethodActionConstraint)) as HttpMethodActionConstraint;

                    if(httpMethodAction != null)
                    {
                        info.HttpMethod = string.Join(",", httpMethodAction.HttpMethods);
                    }
                }

                // Special controller path
                if (info.Path == "/RouteAnalyzer_Main/ShowAllRoutes")
                {
                    info.Path = RouteAnalyzerRouteBuilderExtensions.RouteAnalyzerUrlPath;
                }

                // Additional information of invocation
                info.Invocation += $" ({_e.DisplayName})";

                // Generating List
                ret.Add(info);
            }

            // Result
            return ret;
        }
于 2020-04-18T13:58:49.423 回答
1

试试这个:

// Initialize via constructor dependency injection    
private readonly IActionDescriptorCollectionProvider _provider; 

var info = _provider.ActionDescriptors.Items.Where(x => x.AttributeRouteInfo.Name == "GetUser");
于 2020-04-18T14:09:23.683 回答