简体   繁体   English

ASP.NET Core - CreatedAtRoute() - 如何返回相对位置标头?

[英]ASP.NET Core - CreatedAtRoute() - how to return relative location header?

In an ASP.NET core 6 ApiController I'm using CreatedAtRoute() as a result of a POST API:在 ASP.NET core 6 ApiController 中,我使用CreatedAtRoute()作为 POST API 的结果:

[HttpPost]
[ProducesResponseType(typeof(CatalogItem), (int)HttpStatusCode.Created)]
public async Task<ActionResult<CatalogItem>> CreateNewCatalogItemAsync(CatalogItemDto itemDto)
{
  // ...
  return CreatedAtRoute(nameof(GetCatalogItemByIdAsync), new { itemId = item.Id }, item);
}

This results in a location header with the absolute URL.这会产生一个带有绝对 URL 的位置标头。

So my question is: How can I change this to return a relative URL instead in the Location response header?所以我的问题是:如何更改它以在 Location 响应标头中返回相对 URL?

So instead of Location: http://foo.bar/api/item/1 I'd like to get Location: /api/item/1所以不是Location: http://foo.bar/api/item/1我想得到Location: /api/item/1

Based on the comments there does not seem to be any easy, built-in solution.根据评论,似乎没有任何简单的内置解决方案。 Hence I build the following middleware, based on this post :因此,我根据这篇文章构建了以下中间件:

app.Use(async (context, next) =>
{
    context.Response.OnStarting(o =>
    {
        if (o is HttpContext ctx)
        {
            // In order to get relative location headers (without the host part), we modify any location header here
            // This is to simplify the reverse-proxy setup in front of the application
            try
            {
                if (!string.IsNullOrEmpty(context.Response.Headers.Location))
                {
                    var locationUrl = new Uri(context.Response.Headers.Location);
                    context.Response.Headers.Location = locationUrl.PathAndQuery;
                }
            }
            catch (Exception) { }
        }
        return Task.CompletedTask;
    }, context);
    await next();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core“CreatedAtRoute”失败 - ASP.NET Core “CreatedAtRoute” Failure ASP.NET Core 2.2-复杂路由的CreatedAtRoute方法失败 - ASP.NET Core 2.2 - CreatedAtRoute method failure for complex routes ASP.NET Core CreatedAtRoute没有路由匹配提供的值 - ASP.NET Core CreatedAtRoute No route matches the supplied values ASP.NET Core 2.1 路由 - CreatedAtRoute - 没有路由与提供的值匹配 - ASP.NET Core 2.1 Routing - CreatedAtRoute - No route matches the supplied values ASP.NET Web API CreatedAtRoute返回500内部服务器错误 - ASP.NET Web API CreatedAtRoute return 500 Internal Server Error 如何使用jQuery从ASP.Net Web API CreatedAtRoute获取ID - How to get Id from ASP.Net web api CreatedAtRoute with jquery 如何在ASP.NET Core中设置正确的AttachDbFilename相对路径? - How to set the right AttachDbFilename relative path in ASP.NET Core? 如何在 CreatedAtRoute 位置添加网关基本路径 Header - URL 重写 - How add Gateway base path on CreatedAtRoute Location Header - URL Rewrite 如何在asp.net核心中返回自定义http标头? - How can I return custom http header in asp.net core? 从 ODataController 返回 CreatedAtRoute 位置 - Return CreatedAtRoute location from ODataController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM