;

How to get a URL or extract different parts of a URL in ASP.NET Core


Tutorialsrack 01/03/2022 C# ASP.NET Core

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

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

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

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

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

https://localhost:7289/home/privacy?param1=hello&param2=world&param3=123

Example 1: To Get Host Name

To get the hostname from the URL, you can use the code as given in example:

Example 1: To Get Host Name
var HostName = HttpContext.Request.Host.ToString();
//Output ==> localhost:7289

Example 2: To Get Port

To get the port from the URL, you can use the code as given in example:

Example 2: To Get Port
var Port = HttpContext.Request.Host.Port.ToString();
//Output ==> 7289

Example 3: To Get Path

To get the path from the URL, you can use the code as given in the example:

Example 3: To Get Path
var Path = HttpContext.Request.Path.ToString();
//Output ==> /home/privacy

Example 4: To Get QueryString

To get the QueryString from the URL, you can use the code as given in example:

Example 4: To Get QueryString
var QueryString = HttpContext.Request.QueryString.ToString();
//Output ==> ?param1=hello&param2=world&param3=123

Example 5: To Get URL Scheme

To get the URL scheme(HTTP or HTTPS) from the URL, you can use the code as given in example:

Example 5: To Get URL Scheme
var Scheme = HttpContext.Request.Scheme.ToString();
//Output ==> https

Example 6: To Get Path With QueryString

To get the Path with the QueryString from the URL, you need to use the namespace “Microsoft.AspNetCore.Http.Extensions” and the GetEncodedPathAndQuery() method,  you can use the code as given in example:

Example 6: To Get Path With QueryString
var pathWithQueryString = HttpContext.Request.GetEncodedPathAndQuery().ToString();
//Output ==> /home/privacy?param1=hello&param2=world&param3=123

Example 7: To Get Full URL

To get the complete URL, you need to use the namespace “Microsoft.AspNetCore.Http.Extensions” and the GetDisplayUrl() method, you can use the code as given in example:

Example 7: To Get Full URL using GetDisplayUrl()
var FullURL = HttpContext.Request.GetDisplayUrl().ToString();
//Output ==> https://localhost:7289/home/privacy?param1=hello&param2=world&param3=123

And you can also use this code as given below in the example to get the complete URL:

Example 8: To Get Full URL using GetEncodedUrl()
var encodedUrl = HttpContext.Request.GetEncodedUrl().ToString();
//Output ==> https://localhost:7289/home/privacy?param1=hello&param2=world&param3=123

I tested the above examples in ASP.NET Core 3.1 or a later version.

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

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags