Get the URL of the current page in C# - Asp.Net

string url = HttpContext.Current.Request.Url.AbsoluteUri;

Above code will return the absolute URL . "", for example, or it will return localhost URL if you are in the local environment. It is important to note that Url.AbsoluteUri does return anything after the “#” symbol.

Also you can extract different values from URL using HttpContext.Current.Request

If you just need the part between "http://" and the first slash:

string host = HttpContext.Current.Request.Url.Host;

If you want only the scheme and authority of the request (protocol, host and port):

string authority = HttpContext.Current.Request.Url.Authority;

How to get the URL of the current page in C# - Asp.Net

To get the port no. of the URL:

string port = HttpContext.Current.Request.Url.Port;

To get AbsolutePath :

string absolutepath = HttpContext.Current.Request.Url.AbsolutePath;

To get Application path :

string applicationpath = HttpContext.Current.Request.ApplicationPath;

To get Path and Query :

string pathandquery = HttpContext.Current.Request.Url.PathAndQuery;

To get complete URL of Current page:

string rawurl = HttpContext.Current.Request.RawUrl

What does this code mean?


It takes the url of the current web request, including the query string , and then extracts the part before the query string i.e. the part before the question mark (?).