Search code examples
requesturiasp.net-core

Get raw URL from Microsoft.AspNetCore.Http.HttpRequest


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?


Solution

  • 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!