【问题标题】:Getting tokenized path matched by route template (ASP.NET Core 3.1)获取路由模板匹配的标记化路径(ASP.NET Core 3.1)
【发布时间】:2020-03-22 00:57:10
【问题描述】:

我正在尝试找出获取与路由模板匹配的原始请求 URI 的片段的最佳方法,即 https://apim.example.com/api/v1/controller/[a/{id}/b] 与 [..] 是路由匹配的片段模板;路径可从 this.Request.Path 获得,但片段是否单独可用?

我已阅读(全部)文档,检查了 HttpRequest 实例的所有属性,但没有找到实际的标记化片段(只有描述控制器/操作参数的元数据,如命名等)。我需要片段作为代理 API 的一部分(带有特定于模式的处理程序)。

代理 API

我正在ASP.NET Core 3.1(位于Azure API Management 实例后面)中编写一个代理服务,它映射远程API 的子集并在顶部应用安全方案。代理识别一组已知的路由签名,以便根据请求的资源应用安全方案。

请求发送到 Azure API 管理(例如):

https://apim.example.com/api/v1/organizations/{id}/{*path}

Azure API 管理转发的代理请求:

https://proxy.example.com/api/v1/proxy/v1/organizations/{id}/{*path}

值得一提的是,代理和远程 API 都提供了一个个性化的版本控制方案(因此来自 Azure API 管理的完全限定代理路径)。代理是用ASP.NET Core 3.1 routing实现的,从下面的sn-p可以看出:

[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class ProxyController : ControllerBase
{
    .. constructors and other members omitted for brevity

    [HttpGet]
    [HttpPost]
    [HttpPut]
    [HttpDelete]
    [Route("v1/organizations/{id:int}/{*path}")]
    public async Task OrganizationsAsync(int organizationID, string path, CancellationToken cancellation = default)
    {
        // would like to get the tokenized "v1/organizations/{id:int}/{*path}" fragment here

        var message = await this.service.HandleAsync(this.Request, path, cancellation);

        await this.handler.CopyAsync(this.HttpContext, message);
    }
}

在OrganizationsAsync 中,我正在寻找一种动态方法来获取v1/organizations/{id:int}/{*path} 片段,而不必预定义控制器的各种基本URI(从以下示例中可以看出)。

使用预定义的控制器 URI

[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class ProxyController : ControllerBase
{
    .. constructors and other members omitted for brevity

    private readonly string baseUri = "api/v1/proxy";

    [HttpGet]
    [HttpPost]
    [HttpPut]
    [HttpDelete]
    [Route("v1/organizations/{id:int}/{*path}")]
    public async Task OrganizationsAsync(int organizationID, string path, CancellationToken cancellation = default)
    {
        var localpath = this.Request.Path.Value.Replace(this.baseUri, "");
        var message = await this.service.HandleAsync(this.Request, localpath, cancellation);

        await this.handler.CopyAsync(this.HttpContext, message);
    }
}

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:

    根据 ASP.NET Core 团队的说法,目前没有提供仅匹配路由模板签名的字符串片段(或等效结构)的 API。

    我决定从路由值创建片段,从以下 sn-p 可以看出:

    [ApiController]
    [ApiVersion("1.0")]
    [Route("api/v{apiVersion:apiVersion}/[controller]")]
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    public class ProxyController : ControllerBase
    {
        .. constructors and other members omitted for brevity
    
        [HttpGet]
        [HttpPost]
        [HttpPut]
        [HttpDelete]
        [Authorize]
        [Route("v{version:int}/organizations/{organizationID:int}/{*path}")]
        public async Task OrganizationsAsync(int version, int organizationID, string path, CancellationToken cancellation = default)
        {
            await this.HandleAsync($"v{version}/organizations/{organizationID}/{path}", cancellation);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-21
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 2020-11-15
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 2020-10-31
      相关资源
      最近更新 更多