我正在开发一个 .net 核心应用程序。我正在尝试访问中间件中的路由数据,但它始终返回 null。
这是 Controller 基本路线
[Route("User/{UserId}/Quote")]
public class QuoteController : ControllerBase
我的目标是尝试在中间件中获取 UserId,我将其用于其他地方的身份验证。我的问题是为什么 GetRouteData() 调用总是返回 null?
最佳答案
路由数据由 MVC 中间件设置,该中间件在流的更下游出现。您必须在 await next 调用之后将 context.GetRouteData() 调用移动到该行才能访问路由数据。
看图片https://stackoverflow.com/a/42583583/2108241以供引用。
what is strange is that if I put code after UseMvc(), it does not even get to this line
一旦 MVC 中间件处理了请求,它就会将响应发送回客户端,并且不会处理对管道中下一个中间件的控制。
关于routing - 如何在 .net 核心中间件中获取 RouteData?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51316237/