简体   繁体   English

使用端点路由和 MVC 时,如何从 Endpoint.RequestDelegate 获取 IActionContextAccessor?

[英]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.

相关问题 RequestDelegate 是一个端点吗? - is RequestDelegate an endpoint? 如何在UDPClient上获取远程端点? - How can i get a remote endpoint at UDPClient? 如何在WCF中使用路由表调用端点? - How to call endpoint using routing table in wcf? 从 MVC 迁移到 ASP.NET Core 3.1 中的端点路由时,具有角色的 AuthorizeAttribute 不起作用 - AuthorizeAttribute with Roles not working when migrating from MVC to Endpoint Routing in ASP.NET Core 3.1 使用端点路由时不支持使用“UseMvc”配置 MVC - Using 'UseMvc' to configure MVC is not supported while using Endpoint Routing 在 ASP.Net Core 2.2 MVC 中使用 Endpoint-Routing 时如何正确覆盖 IUrlHelper? - How top correctly override the IUrlHelper while using Endpoint-Routing in ASP.Net Core 2.2 MVC? 使用HttpClient,如何在更改端点的循环中发送GET请求并返回响应 - Using HttpClient how can I send GET requests and return a response in a loop that changes the endpoint 如何使用本地 json 文件从模拟端点获取结果? - How can I get the results from mock a endpoint with a local json file? 调用PUT(编辑)端点时,如何从模型中省略变量? - How can I omit a variable from a model when calling a PUT (edit) endpoint? 如何使用 .net 4 api 端点从 Request.Content 对象获取原始请求正文 - How do I get the raw request body from the Request.Content object using .net 4 api endpoint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM