Header Scripts

You may need to get the URL from an ASP.NET web page in C#. Here are some examples on how to do this.

Example:

http://localhost:78000/Sandbox/Default.aspx?q1=1&q2=2

Code:

$host = HttpContext.Current.Request.Url.Host;
$authority = HttpContext.Current.Request.Url.Authority;
$port = HttpContext.Current.Request.Url.Port;
$absolutePath = HttpContext.Current.Request.Url.AbsolutePath;
$applicationPath = HttpContext.Current.Request.ApplicationPath;
$absoluteURI = HttpContext.Current.Request.Url.AbsoluteUri;
$pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;

Response.Write($"Host: {host}<br />Authority: {$authority}<br />Port: {$port}<br />Absolute Path: {$absolutePath}<br />Application Path: {$applicationPath}<br />Absolute URI: {$absoluteURL}<br />Path and Query: {$pathAndQuery}");

Output:

Host: localhost
Authority: localhost:78000
Port: 78000
Absolute Path: /Sandbox/Default.aspx
Application Path: /Sandbox
Absolute URI: http://localhost:78000/Sandbox/Default.aspx?q1=1&q2=2
Path and Query: /Sandbox/Default.aspx?q1=1&q2=2

Print Friendly, PDF & Email
Translate ยป