/Results.Created-Issue

Demonstrate issue with Results.Created in ASP.NET Core 7 Minimal APIs cf: https://github.com/dotnet/aspnetcore/issues/45101

Primary LanguageC#MIT LicenseMIT

Results.Created returns a relative URI in the location header instead of an absolute (as in classic Web API)

Using the standard template

dotnet new webapi --output Issue -minimal

with the following setup:

app.MapPost("/weatherforecast", (WeatherForecast forecast) =>
{
    var id = 42;
    return TypedResults.Created($"/weatherforecast/{id}", forecast);
})
.WithName("PostWeatherForecast");

and calling the API with:

POST http://localhost:5268/weatherforecast
{
  "date": "2022-11-15",
  "temperatureC": 10,
  "summary": "Cool"
}

yields the following headers:

content-type: application/json; charset=utf-8
date: Tue,15 Nov 2022 08:10:56 GMT
location: /weatherforecast/42
server: Kestrel
transfer-encoding: chunked

Mapping with group

var weather = app.MapGroup("/api/weatherforecast").WithOpenApi();

weather.MapPost("", (WeatherForecast forecast) =>
{
    var id = 42;
    return TypedResults.Created($"{id}", forecast);
})
.WithName("PostApiWeatherForecast")
.WithOpenApi();

and calling the API with:

POST http://localhost:5268/api/weatherforecast
{
  "date": "2022-11-15",
  "temperatureC": 10,
  "summary": "Cool"
}

yields the following headers:

content-type: application/json; charset=utf-8
date: Tue,15 Nov 2022 08:11:26 GMT
location: 42
server: Kestrel
transfer-encoding: chunked

Expected Behavior

In both cases above I would expect an absolute URI in the location header:

http://localhost:5268/weatherforecast/42

and

http://localhost:5268/api/weatherforecast/42