asp net c# get page url

Get Page URL in ASP.NET C#

By David Lewis
AD

In the dynamic world of ASP.NET web development using C#, acquiring the current page's URL is a common requirement. Luckily, .NET Framework simplifies this task with straightforward methods. Let's delve into how to fetch the page URL in ASP.NET using C#.

Understanding the URL's importance is the first step. It comprises various components like protocol, domain name, parameters, etc. Fetcing the page URL is useful for tasks such as logging, user redirection, or analytics tracking.

Retrieving URL in ASP.NET

The 'Url' class within the 'System.Web' namespace offers several methods to retrieve a URL in ASP.NET.

AD
ASP.NET Core MVC Training in Vadodara - ATI
ASP.NET Core MVC Training in Vadodara - ATI
a screenshot of an open source program
a screenshot of an open source program
🚀 HTTP Verbs Explained for Beginners | ASP.NET Core Web API
🚀 HTTP Verbs Explained for Beginners | ASP.NET Core Web API

Some popular methods include 'RawUrl', 'Path', 'Query', and 'AbsoluteUri', each providing different aspects of the URL. We'll explore each in detail below.

Using RawUrl

The 'Url.RawUrl' property returns the URL as it was received from the browser. It includes the path and query string but excludes the host and protocol. This method is best suited for scenarios where you need just the path and query parts of the URL.

Asp.net Core Roadmap For Beginners
Asp.net Core Roadmap For Beginners
ASP.Net Projects with Source Code
ASP.Net Projects with Source Code
Understanding OOPS in ASP.NET Core | MOHAMED HASIF M H posted on the topic | LinkedIn
Understanding OOPS in ASP.NET Core | MOHAMED HASIF M H posted on the topic | LinkedIn

Example: string rawUrl = HttpContext.Current.Request.Url.RawUrl;

Using Path

The 'Url.Path' property retrieves only the file path of the current page without the query string. This is useful when you need only the server relative path of the URL.

AD

Example: string path = HttpContext.Current.Request.Url.LocalPath;

What is the difference between ASP.NET and ASP.NET Core?
What is the difference between ASP.NET and ASP.NET Core?
Asp net
Asp net
GitHub - MoienTajik/AspNetCore-Developer-Roadmap: Roadmap to becoming an ASP.NET Core developer in 2026
GitHub - MoienTajik/AspNetCore-Developer-Roadmap: Roadmap to becoming an ASP.NET Core developer in 2026
the asp net page lifecycle
the asp net page lifecycle
Why choose ASP.Net for Web Application Development
Why choose ASP.Net for Web Application Development
#ASP.Net Core benefits
#ASP.Net Core benefits

Getting Full URL

There are scenarios where you might need the full URL, including protocol, hostname, and port. The 'AbsoluteUri' property fulfills this requirement.

This is particularly useful when you want to construct a complete hyperlink to the current page.

AD

Using AbsoluteUri

'Url.AbsoluteUri' returns the full URL, including the scheme (http or https), the hostname, the port (if specified), and the file path with query string. It represents the full URL as a string.

Asp.Net Web Application Development in Singapore
Asp.Net Web Application Development in Singapore

Example: string absoluteUri = HttpContext.Current.Request.Url.AbsoluteUri;

Using HttpContext Utility

Besides, you can leverage the 'HttpContext' class' utility methods, specifically 'Request.Url', to fetch the URL easily. This method encapsulates the 'Url' class and simplifies usage.

AD

Example: string url = HttpContext.Current.Request.Url.ToString();

In ASP.NET, various methods and properties enable seamless retrieval of the current page's URL. Depending on your need, you can choose from 'RawUrl', 'Path', 'Query', or 'AbsoluteUri'. Leveraging these, you can effectively work with URLs in your C# code.

Remember, URL handling is integral to web development, and mastering URL retrieval in ASP.NET will boost your productivity and proficiency. So, keep practicing and exploring to become an ASP.NET pro!

AD