![](/img/trans.png)
[英]ASP.NET Core - CreatedAtRoute() - how to return relative location header?
[英]How add Gateway base path on CreatedAtRoute Location Header - URL Rewrite
我有一个 Ocelot 网关配置如下:
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{version}/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 3010
}
],
"UpstreamPathTemplate": "/serviceName/api/{version}/{everything}",
"UpstreamHttpMethod": [ "POST", "PATCH", "PUT", "GET", "DELETE" ]
}
}
我有以下 controller
[Produces("application/json")]
[Route("api/v1/[controller]")]
[ApiController]
public class NameController : ControllerBase
{
[HttpPost()]
public async Task<IActionResult> Create([FromForm]Request request, CancellationToken cancellationToken)
{
// create something..
return CreatedAtRoute("Get", new { id }, string.Empty);
}
[HttpGet("{id}", Name = "Get")]
public async Task<IActionResult> Get([FromRoute] string id, CancellationToken cancellationToken)
{
// return something
}
}
我希望我的 Location header 是{domain}/serviceName/api/v1/Name/{id}
,但是它返回{domain}/api/v1/Name/{id}
。
任何人都知道如何使用 CreatedAtRoute 进行 URL 重写,好吗?
我通过阅读此处的 Ocelot 文档找到了解决方案。
基本上你需要添加 Header 转换:
{
"ReRoutes": [
{
"DownstreamHeaderTransform": {
"Location": "{DownstreamBaseUrl}, {BaseUrl}/serviceName"
},
"HttpHandlerOptions": {
"AllowAutoRedirect": false
},
"DownstreamPathTemplate": "/api/{version}/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 3010
}
],
"UpstreamPathTemplate": "/serviceName/api/{version}/{everything}",
"UpstreamHttpMethod": [ "POST", "PATCH", "PUT", "GET", "DELETE" ]
}
],
"GlobalConfiguration": {
"BaseUrl": "https://api.mybusiness.com"
}
}
有关为不同环境设置 BaseUrl 的更多信息,请参见此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.