从另一个获取动作的 ActionContext

Getting ActionContext of an action from another

我可以获取 ActionContext 或 ActionDescriptor 或可以描述基于路由名称的特定操作的东西吗?

拥有以下控制器。

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

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

我想做的是在调用 "Get" 时,能够访问 "GetUser" 元数据,如动词、路由参数等

类似于

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

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

这个想法中的东西。

有一个 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;
        }

试试这个:

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

var info = _provider.ActionDescriptors.Items.Where(x => x.AttributeRouteInfo.Name == "GetUser");