简体   繁体   中英

How can I get the IActionContextAccessor from the Endpoint.RequestDelegate when using Endpoint Routing and MVC?

I've got a asp .net core 3.1 application and have configured MVC and Endpoint Routing. Assume I have an Endpoint object (it won't always be the Endpoint associated with the current request), I then have it's RequestDelegate. I'd like to get the IActionContextAccessor from this RequestDelegate. In the following example, when I'm in debug mode I can see the _actionContextAccessor so I know it's there.

var endpoint = this.httpContextAccessor.HttpContext.GetEndpoint();

在此处输入图片说明

I'm sure you'd like more context of what I'm doing and I can give you more if you like but the gist of this question is to assume I have an Endpoint object and I'm configured to use MVC, how can I get the IActionContextAccessor?

UPDATE

What I'm ultimately trying to get is the actions parameters. Just the type of the input parameters. We follow a one input model convention so actually what I want is that one input model's type.

To determine the type of an action's parameters, there's no need to use IActionContextAccessor or the ActionContext property it exposes. An Endpoint instance contains a set of metadata: for an endpoint that represents an action, this contains an instance of ActionDescriptor , which, unsurprisingly, describes an action. One of its properties is Parameters , which exposes the set of parameters for that action.

Putting that all together, here's an example of how to get to the type of a single action parameter, as requested:

var actionDescriptor = endpoint.Metadata.GetMetadata<ActionDescriptor>();

if (actionDescriptor != null)
{
    var actionParameterType = actionDescriptor.Parameters.SingleOrDefault()?.ParameterType;

    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM