RedWolves
RedWolves

Reputation: 10395

How do I get the full url of the page I am on in C#

I need to be able to get at the full URL of the page I am on from a user control. Is it just a matter of concatenating a bunch of Request variables together? If so which ones? Or is there a more simpiler way?

Upvotes: 179

Views: 232959

Answers (11)

Michael Freidgeim
Michael Freidgeim

Reputation: 28511

In .Net Core or .Net 5+ for HttpRequest class Request.Url is not available, to have full url use [GetDisplayUrl][2]

using Microsoft.AspNetCore.Http.Extensions; 
var fullUrl= Request.GetDisplayUrl();

Upvotes: 0

Serj Sagan
Serj Sagan

Reputation: 30267

For ASP.NET Core you'll need to spell it out:

var request = Context.Request;
@($"{ request.Scheme }://{ request.Host }{ request.Path }{ request.QueryString }")

Or you can add a using statement to your view:

@using Microsoft.AspNetCore.Http.Extensions

then

@Context.Request.GetDisplayUrl()

The _ViewImports.cshtml might be a better place for that @using

Upvotes: 35

Abhishek Kanrar
Abhishek Kanrar

Reputation: 496

Try the following -

var FullUrl = Request.Url.AbsolutePath.ToString();
var ID = FullUrl.Split('/').Last();

Upvotes: 1

DevelopingChris
DevelopingChris

Reputation: 40808

Request.Url.AbsoluteUri

This property does everything you need, all in one succinct call.

Upvotes: 77

Artem
Artem

Reputation: 71

Better to use Request.Url.OriginalString than Request.Url.ToString() (according to MSDN)

Upvotes: 7

Mohsen
Mohsen

Reputation: 3589

Here is a list I normally refer to for this type of information:

Request.ApplicationPath :   /virtual_dir
Request.CurrentExecutionFilePath :  /virtual_dir/webapp/page.aspx
Request.FilePath :  /virtual_dir/webapp/page.aspx
Request.Path :  /virtual_dir/webapp/page.aspx
Request.PhysicalApplicationPath :   d:\Inetpub\wwwroot\virtual_dir\
Request.QueryString :   /virtual_dir/webapp/page.aspx?q=qvalue
Request.Url.AbsolutePath :  /virtual_dir/webapp/page.aspx
Request.Url.AbsoluteUri :   http://localhost:2000/virtual_dir/webapp/page.aspx?q=qvalue
Request.Url.Host :  localhost
Request.Url.Authority : localhost:80
Request.Url.LocalPath : /virtual_dir/webapp/page.aspx
Request.Url.PathAndQuery :  /virtual_dir/webapp/page.aspx?q=qvalue
Request.Url.Port :  80
Request.Url.Query : ?q=qvalue
Request.Url.Scheme :    http
Request.Url.Segments :  /
    virtual_dir/
    webapp/
    page.aspx

Hopefully you will find this useful!

Upvotes: 357

IonB
IonB

Reputation: 11

If you need the port number also, you can use

Request.Url.Authority

Example:

string url = Request.Url.Authority + HttpContext.Current.Request.RawUrl.ToString();

if (Request.ServerVariables["HTTPS"] == "on")
{
    url = "https://" + url;
}
else 
{
    url = "http://" + url;
}

Upvotes: 1

travis
travis

Reputation: 36483

I usually use Request.Url.ToString() to get the full url (including querystring), no concatenation required.

Upvotes: 161

RedWolves
RedWolves

Reputation: 10395

Thanks guys, I used a combination of both your answers @Christian and @Jonathan for my specific need.

"http://" + Request.ServerVariables["SERVER_NAME"] +  Request.RawUrl.ToString()

I don't need to worry about secure http, needed the servername variable and the RawUrl handles the path from the domain name and includes the querystring if present.

Upvotes: 5

Christian Hagelid
Christian Hagelid

Reputation: 8355

if you need the full URL as everything from the http to the querystring you will need to concatenate the following variables

Request.ServerVariables("HTTPS") // to check if it's HTTP or HTTPS
Request.ServerVariables("SERVER_NAME") 
Request.ServerVariables("SCRIPT_NAME") 
Request.ServerVariables("QUERY_STRING")

Upvotes: 9

FlySwat
FlySwat

Reputation: 175733

Request.RawUrl

Upvotes: 8

Related Questions