;

How to get a URL or extracting different parts of URL in ASP.NET C#


Tutorialsrack 22/06/2019 C#

In this article, we will learn how to get a URL or extracting different parts of URL in ASP.NET C#.

You may at times need to get different parts or values from URL.

Below is some example that shows different ways of extracting different parts of URL in ASP.NET.

Here, we are using URL given below to get different values from URL.

https://localhost:44399/home/index?QueryString1=1&QueryString2=2

To get this part of the URL “https://localhost:44399

Example 1
string URLWithHTTPandPort= Request.Url.GetLeftPart(UriPartial.Authority);
//The above code will return this part of URL:-  https://localhost:44399

To get this part of the URL “localhost

Example 2
string URLHost = System.Web.HttpContext.Current.Request.Url.Host;
//The above code will return this part of URL:-  localhost

To get this part of the URL “localhost:44399

Example 3
string URLAuthority = System.Web.HttpContext.Current.Request.Url.Authority;
//The above code will return this part of URL:-  localhost:44399

To get the port no. of the URL “44399

Example 4
string Port = System.Web.HttpContext.Current.Request.Url.Port;
//The above code will return the port no.:-  44399

To get this port no. of the URL “/home/index

Example 5
string AbsolutePath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
//The above code will return this part of URL:-  /home/index

To get this part of the URL “/

Example 6
string ApplicationPath = System.Web.HttpContext.Current.Request.ApplicationPath;
//The above code will return this part of URL:-  /

To get the absolute URL “https://localhost:44399/home/index?QueryString1=1&QueryString2=2

Example 7
string AbsoluteUri = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
//The above code will return the absolute URL:-  
//https://localhost:44399/home/index?QueryString1=1&QueryString2=2

To get this part of the URL “/home/index?QueryString1=1&QueryString2=2

Example 8
string PathAndQuery = System.Web.HttpContext.Current.Request.Url.PathAndQuery;
//The above code will return this part of URL:-  /home/index?QueryString1=1&QueryString2=2

You can read more about URL properties here.

I hope this article will help you to understand how to get a URL or extracting different parts of URL in ASP.NET C#

Share your valuable feedback and help us to improve. If you find anything incorrect, or you want to share more information about the topic discussed above. please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags