146

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?

pbristow
  • 1,997
  • 4
  • 26
  • 46
Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
  • 1
    A bug seems to have been filed earlier about this but closed...you can probably check the details of it and if you feel stronger about it, may be update it with details: https://github.com/aspnet/HttpAbstractions/issues/110 – Kiran Jan 24 '15 at 19:30
  • @KiranChalla: I sort of take their point, although it does lead me to wonder what the RawURL is in previous versions then. I guess what they are currently showing about the scheme, host etc can be divined from the server side handling of the request, and not anything on the request itself. – Jon Egerton Jan 24 '15 at 22:05
  • did you try ToString() ? – agua from mars Feb 25 '15 at 16:27

9 Answers9

123

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.

Leo Hendry
  • 1,423
  • 1
  • 8
  • 6
Velin Georgiev
  • 2,359
  • 1
  • 15
  • 21
  • Is ASP.NET Core RC2 available now? – SerjG May 15 '16 at 20:00
  • Yes - https://blogs.msdn.microsoft.com/webdev/2016/05/16/announcing-asp-net-core-rc2/ – Velin Georgiev May 16 '16 at 20:41
  • Looking at source, these clearly do some encoding/decoding so this will not be the raw url. Also, IIS will change sometimes change the url before it gets to Kestrel e.g. %2F -> /. – Daniel Leach Dec 08 '17 at 13:30
  • NO NO - This will not work. NEITHER `GetEncodedUrl()` nor `GetDisplayUrl()` are available methods anymore. – Tom Stickel Dec 28 '18 at 22:42
  • 1
    @TomStickel Not sure what you're talking about... I had no issue using either of them. Make sure you have the `using` directive in you file as described in the answer, as these are not "normal" methods, but rather extension methods. – Nick Mertin Feb 19 '19 at 01:35
  • @NickMertin - I'm not in front of my Core application so I can't tell you which type of application, but I will say that there are so many times that things are abstracted in an existing application that doing a migration - I didn't have the ability to even include certain namespaces. Example: you simply cannot get all the same namespaces in a class library core project that you can get in a mvc core application project. I know this reply is vague - but I end up writing a lot of middleware to get around the changes presented in .net core - also .net core 2.2 has changed from 2.1 etc.. – Tom Stickel Feb 19 '19 at 04:53
  • 1
    @TomStickel fair. Just noting that with the Microsoft.AspNetCore.All package installed for ASP.NET Core 2.2 (also tested on 2.0), this works fine for me. – Nick Mertin Feb 19 '19 at 13:35
116

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!

Matt DeKrey
  • 11,582
  • 5
  • 54
  • 69
  • 8
    This no longer works as of beta-5. I do not have a good alternative or would update my answer. – Matt DeKrey May 22 '15 at 20:37
  • 28
    I believe this was made a true extension method - you simply import the namespace and call either `GetEncodedUri` or `GetDisplayUri`, depending on your use case. – dlras2 Sep 17 '15 at 14:34
  • 53
    using Microsoft.AspNet.Http.Extensions; and that Request.GetDisplayUrl() – mswietlicki Feb 11 '16 at 16:03
  • 9
    The right namespace is now Microsoft.AspNetCore.Http.Extensions – C-F Jul 31 '16 at 09:04
  • 10
    For ASP.NET Core 1.0 add the using "Microsoft.AspNetCore.Http.Extensions" to your Razor view. To get the url use "@Context.Request.GetDisplayUrl()". – Joop Aug 31 '16 at 07:08
  • 3
    For Azure Functions: add reference `#r "Microsoft.AspNetCore.Http.Extensions"`, add `using Microsoft.AspNet.Http.Extensions;` and use like `UriHelper.GetDisplayUrl(req)` or `UriHelper.GetEncodedUrl(req)` as required. – Gonzalo Lucero Oct 17 '18 at 19:40
34

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.

khellang
  • 17,550
  • 6
  • 64
  • 84
24

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
Shadi Alnamrouti
  • 11,796
  • 4
  • 56
  • 54
18

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;
}
giammin
  • 18,620
  • 8
  • 71
  • 89
  • 1
    Good one. Also i improved your solution with optional parameters. Therefore i can control which part of URI i want to retreive. For example, host only or full path without query string etc. – Sam Alekseev Feb 21 '19 at 12:10
  • @user3172616 nice idea! – giammin Feb 22 '19 at 10:24
  • 2
    `(80)` should be `(-1)`. When you have https scheme with port omitted in the "Host" header this will generate wrong Uri (e.g. `https://myweb:80/`, with `(-1)` it will be `https://myweb`). – Igor Dražić Nov 05 '19 at 14:00
  • using `request.Host.Value` would be better, and you're missing `request.PathBase` if the app uses a `PathBase`. Also, it would be better to use `.Value` for the QueryString :) – Mafii Aug 14 '23 at 08:15
5

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;
}
jor
  • 2,058
  • 2
  • 26
  • 46
Sam
  • 4,694
  • 2
  • 36
  • 47
5

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}";
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mark Redman
  • 24,079
  • 20
  • 92
  • 147
1

For me the best way to retrieve the base URL on .NET Core 6

using Microsoft.AspNetCore.Http.Extensions;

var url = request.GetDisplayUrl();
var path = request.Path.ToString();
                      
string baseUrl = url.Replace(path, "");
RenR
  • 31
  • 3
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 24 '22 at 00:33
0

In ASP.NET 5 beta5:

Microsoft.AspNet.Http.Extensions.UriHelper.Encode(
    request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString);
Smartkid
  • 1,772
  • 2
  • 25
  • 34