【问题标题】:How to Get Route Data from Middleware for ASPNETCORE OData request?如何从中间件获取 ASPNETCORE OData 请求的路由数据?
【发布时间】:2018-10-31 15:32:12
【问题描述】:

我有以下中间件类,用于获取当前请求的路由数据。

如果您在 Values Controller 上执行 Get,您将看到 context.GetRouteData() 返回一个值并且不为空。我已将 Swagger\Swashbuckle 连接到附加的项目中,以使其更易于演示。

但是,如果您调用 Blog OData 控制器 (http://localhost:40946/odata/Blog),context.GetRouteData() 将返回 null。

有谁知道如何从中间件访问 RouteData 以获取 OData 请求?

public class TestMiddleware
{
    protected readonly RequestDelegate _next;

    public TestMiddleware(RequestDelegate next)
    {
        this._next = next;
    }

    public virtual async Task Invoke(HttpContext context)
    {
        var routeData = context.GetRouteData();

        await this._next.Invoke(context);
    }
}

Link to sample solution demonstrating the issue.

【问题讨论】:

    标签: asp.net-core odata asp.net-core-middleware


    【解决方案1】:

    看起来 OData 中间件没有将路由信息作为 IRoutingFeature 提供给上下文。 Link to Source Code

    不确定根本原因 - 但作为一种解决方法,您可以使用

    var httpRequestFeature = context.Features[typeof(IHttpRequestFeature)] as IHttpRequestFeature;
    var path = httpRequestFeature?.Path;
    

    【讨论】:

      猜你喜欢
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      相关资源
      最近更新 更多