[英]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.我有一个 asp .net core 3.1 应用程序并配置了 MVC 和端点路由。 Assume I have an Endpoint object (it won't always be the Endpoint associated with the current request), I then have it's RequestDelegate.
假设我有一个 Endpoint 对象(它不会总是与当前请求关联的 Endpoint),然后我有它的 RequestDelegate。 I'd like to get the IActionContextAccessor from this RequestDelegate.
我想从这个 RequestDelegate 获取 IActionContextAccessor。 In the following example, when I'm in debug mode I can see the _actionContextAccessor so I know it's there.
在以下示例中,当我处于调试模式时,我可以看到 _actionContextAccessor,因此我知道它在那里。
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?我确定你想要更多关于我正在做的事情的上下文,如果你愿意,我可以给你更多,但这个问题的要点是假设我有一个 Endpoint 对象并且我被配置为使用 MVC,怎么能我得到了 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.要确定操作参数的类型,无需使用
IActionContextAccessor
或它公开的ActionContext
属性。 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.一个
Endpoint
实例包含一组元数据:对于代表一个动作的端点,它包含一个ActionDescriptor
的实例, ActionDescriptor
,它描述了一个动作。 One of its properties is Parameters
, which exposes the set of parameters for that action.它的一个属性是
Parameters
,它公开了该操作的参数集。
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;
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.