What is the ASP.NET Core MVC equivalent to Request.RequestURI?

I found a blog post that shows how to “shim” familiar things like HttpResponseMessage back into ASP.NET Core MVC, but I want to know what’s the new native way to do the same thing as the following code in a REST Post method in a Controller:

// POST audit/values
[HttpPost]
public System.Net.Http.HttpResponseMessage Post([FromBody]string value)
{
    var NewEntity = _repository.InsertFromString(value);

    var msg = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Created);
    msg.Headers.Location = new Uri(Request.RequestUri + NewEntity.ID.ToString());
    return msg;

}

In an ASP.NET Core MVC project, I can’t seem to get Request.RequestUri.

I tried inspecting Request, and I was able to make a function like this:

private string UriStr(HttpRequest Request)
{
    return Request.Scheme + "://" + Request.Host + Request.Path; // Request.Path has leading /
}

So I could write UriStr(Request) instead. But I’m not sure that’s right. I feel like I’m hacking my way around, and not using this correctly.

A related question for earlier non-Core ASP.NET MVC versions asks how to get the base url of the site.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Personally, I use :

new Uri(request.GetDisplayUrl())
  • GetDisplayUrl fully un-escaped form (except for the QueryString)
  • GetEncodedUrl – fully escaped form suitable for use in HTTP headers

These are extension method from the following namespace : Microsoft.AspNetCore.Http.Extensions

Method 2

A cleaner way would be to use a UriBuilder:

private static Uri GetUri(HttpRequest request)
{
    var builder = new UriBuilder();
    builder.Scheme = request.Scheme;
    builder.Host = request.Host.Value;
    builder.Path = request.Path;
    builder.Query = request.QueryString.ToUriComponent();
    return builder.Uri;
}

(not tested, the code might require a few adjustments)

Method 3

Here’s a working code. This is based off @Thomas Levesque answer which didn’t work well when the request is from a custom port.

public static class HttpRequestExtensions
{
    public static Uri ToUri(this HttpRequest request)
    {
        var hostComponents = request.Host.ToUriComponent().Split(':');

        var builder = new UriBuilder
        {
            Scheme = request.Scheme,
            Host = hostComponents[0],
            Path = request.Path,
            Query = request.QueryString.ToUriComponent()
        };

        if (hostComponents.Length == 2)
        {
            builder.Port = Convert.ToInt32(hostComponents[1]);
        }

        return builder.Uri;
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x