0

In my Controller I can't use CreatedAtAction for reasons out of the scope of this question. I therefore need to do the following, however I don't know how to get the absolute URL of the Web API to correctly set the Location header?

public async Task<ActionResult<string>> PostItem(ItemPostRequest itemPostRequest)
{
    // call repository etc.

    string baseUrl= // <------- how to get this?
    Response.Headers.Add("Location", baseUrl + insertId);

    return Content(savedItem, "application/json", System.Text.Encoding.UTF8);
}

I am on .NET 5.0, in case this matters.

0

1 Answer 1

4
string baseUrl = $"{Request.Scheme}://{Request.Host.Value}/{Request.Path}";
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, that looks promising after first tests. Note that Request.Path can contain a trailing slash. Wondering if one has to check this manually or there is a more comfortable solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.