Accessing the Current Page URL in ASP.NET C#
This guide demonstrates how to obtain the URL of the currently displayed page within an ASP.NET application using C#.
Scenario:
Often, ASP.NET C# developers need to retrieve the current page's URL for various tasks.
Method:
The HttpRequest
object offers several properties to access different parts of the URL:
<code class="language-csharp">string fullUrl = HttpContext.Current.Request.Url.AbsoluteUri; string pagePath = HttpContext.Current.Request.Url.AbsolutePath; string serverHost = HttpContext.Current.Request.Url.Host;</code>
Details:
AbsoluteUri
: Returns the complete, absolute URL of the requested resource.AbsolutePath
: Provides the path portion of the URL, excluding the server's hostname.Host
: Specifies the hostname or IP address of the web server.These properties allow you to extract specific URL components, offering flexibility in your code.
The above is the detailed content of How to Get the Current Page URL in ASP.NET C#?. For more information, please follow other related articles on the PHP Chinese website!