簡體   English   中英

從另一個獲取一個動作的 ActionContext

[英]Getting ActionContext of an action from another

我可以獲得一個 ActionContext 或 ActionDescriptor 或可以根據路由名稱描述特定操作的東西嗎?

具有以下 controller。

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 package, 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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM