Get raw URL from Microsoft.AspNetCore.Http.HttpRequest

RequestUriasp.net Core

Request Problem Overview


The HttpRequest class in Asp.Net 5 (vNext) contains (amongst other things) parsed details about the URL for the request, such as Scheme, Host, Path etc.

I've haven't spotted anywhere yet that exposes the original request URL though - only these parsed values. (In previous versions there was Request.Uri)

Can I get the raw URL back without having to piece it together from the components available on HttpRequest?

Request Solutions


Solution 1 - Request

It looks like you can't access it directly, but you can build it using the framework:

Microsoft.AspNetCore.Http.Extensions.UriHelper.GetFullUrl(Request)

You can also use the above as an extension method.

This returns a string rather than a Uri, but it should serve the purpose! (This also seems to serve the role of the UriBuilder, too.)

Thanks to @mswietlicki for pointing out that it's just been refactored rather than missing! And also to @C-F to point out the namespace change in my answer!

Solution 2 - Request

Add the Nuget package / using:

using Microsoft.AspNetCore.Http.Extensions; 

(In ASP.NET Core RC1 this was in Microsoft.AspNet.Http.Extensions)

then you can get the full http request url by executing:

var url = httpContext.Request.GetEncodedUrl();

or

var url = httpContext.Request.GetDisplayUrl();

depending on the purposes.

Solution 3 - Request

If you really want the actual, raw URL, you could use the following extension method:

public static class HttpRequestExtensions
{
    public static Uri GetRawUrl(this HttpRequest request)
    {
        var httpContext = request.HttpContext;

        var requestFeature = httpContext.Features.Get<IHttpRequestFeature>();

        return new Uri(requestFeature.RawTarget);
    }
}

This method utilizes the RawTarget of the request, which isn't surfaced on the HttpRequest object itself. This property was added in the 1.0.0 release of ASP.NET Core. Make sure you're running that or a newer version.

NOTE! This property exposes the raw URL, so it hasn't been decoded, as noted by the documentation:

> This property is not used internally for routing or authorization decisions. It has not been UrlDecoded and care should be taken in its use.

Solution 4 - Request

In .NET Core razor:

@using Microsoft.AspNetCore.Http.Extensions
@Context.Request.GetEncodedUrl() //Use for any purpose (encoded for safe automation)

You can also use instead of the second line:

@Context.Request.GetDisplayUrl() //Use to display the URL only

Solution 5 - Request

The other solutions did not fit well my needs because I wanted directly an URI object and I think it is better to avoid string concatenation (also) in this case so I created this extension methods than use a UriBuilder and works also with urls like http://localhost:2050:

public static Uri GetUri(this HttpRequest request)
{
    var uriBuilder = new UriBuilder
    {
        Scheme = request.Scheme,
        Host = request.Host.Host,
        Port = request.Host.Port.GetValueOrDefault(80),
        Path = request.Path.ToString(),
        Query = request.QueryString.ToString()
    };
    return uriBuilder.Uri;
}

Solution 6 - Request

The following extension method reproduces the logic from the pre-beta5 UriHelper:

public static string RawUrl(this HttpRequest request) {
    if (string.IsNullOrEmpty(request.Scheme)) {
        throw new InvalidOperationException("Missing Scheme");
    }
    if (!request.Host.HasValue) {
        throw new InvalidOperationException("Missing Host");
    }
    string path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/";
    return request.Scheme + "://" + request.Host + path + request.QueryString;
}

Solution 7 - Request

This extension works for me:

using Microsoft.AspNetCore.Http;

public static class HttpRequestExtensions
{
    public static string GetRawUrl(this HttpRequest request)
    {
        var httpContext = request.HttpContext;
        return $"{httpContext.Request.Scheme}://{httpContext.Request.Host}{httpContext.Request.Path}{httpContext.Request.QueryString}";
    }
}

Solution 8 - Request

In ASP.NET 5 beta5:

Microsoft.AspNet.Http.Extensions.UriHelper.Encode(
    request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJon EgertonView Question on Stackoverflow
Solution 1 - RequestMatt DeKreyView Answer on Stackoverflow
Solution 2 - RequestVelin GeorgievView Answer on Stackoverflow
Solution 3 - RequestkhellangView Answer on Stackoverflow
Solution 4 - RequestShadi NamroutiView Answer on Stackoverflow
Solution 5 - RequestgiamminView Answer on Stackoverflow
Solution 6 - RequestSamView Answer on Stackoverflow
Solution 7 - RequestMark RedmanView Answer on Stackoverflow
Solution 8 - RequestSmartkidView Answer on Stackoverflow