【发布时间】: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