Can anyone help out me in getting the URL of the current working page of ASP.NET in C#?
59 Answers
Try this :
string url = HttpContext.Current.Request.Url.AbsoluteUri; // string path = HttpContext.Current.Request.Url.AbsolutePath; // /TESTERS/Default6.aspx string host = HttpContext.Current.Request.Url.Host; // localhost 11You may at times need to get different values from URL.
Below example shows different ways of extracting different parts of URL
EXAMPLE: (Sample URL)
CODE
Response.Write("<br/>Host " + HttpContext.Current.Request.Url.Host); Response.Write("<br/>Authority: " + HttpContext.Current.Request.Url.Authority); Response.Write("<br/>Port: " + HttpContext.Current.Request.Url.Port); Response.Write("<br/>AbsolutePath: " + HttpContext.Current.Request.Url.AbsolutePath); Response.Write("<br/>ApplicationPath: " + HttpContext.Current.Request.ApplicationPath); Response.Write("<br/>AbsoluteUri: " + HttpContext.Current.Request.Url.AbsoluteUri); Response.Write("<br/>PathAndQuery: " + HttpContext.Current.Request.Url.PathAndQuery); OUTPUT
Host: localhost Authority: localhost:60527 Port: 60527 AbsolutePath: /WebSite1test/Default2.aspx ApplicationPath: /WebSite1test AbsoluteUri: PathAndQuery: /WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2 You can copy paste above sample code & run it in asp.net web form application with different URL.
I also recommend reading ASP.Net Routing in case you may use ASP Routing then you don't need to use traditional URL with query string.
2Just sharing as this was my solution thanks to Canavar's post.
If you have something like this:
"" or like this:
"" and you only want the part that a user would type in then this will work:
String strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery; String strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/"); which would result in these:
"" "" 4if you just want the part between http:// and the first slash
string url = Request.Url.Host; would return stackoverflow.com if called from this page
Here's the complete breakdown
3the request.rawurl will gives the content of current page it gives the exact path that you required
use HttpContext.Current.Request.RawUrl
If you want to get
localhost:2806 from
then use:
HttpContext.Current.Request.Url.Authority a tip for people who needs the path/url in global.asax file;
If you need to run this in global.asax > Application_Start and you app pool mode is integrated then you will receive the error below:
Request is not available in this context exception in Application_Start.
In that case you need to use this:
System.Web.HttpRuntime.AppDomainAppVirtualPath
Hope will help others..
A search landed me at this page, but it wasn't quite what I was looking for. Posting here in case someone else looking for what I was lands at this page too.
There is two ways to do it if you only have a string value.
.NET way:
Same as @Canavar, but you can instantiate a new Uri Object
String URL = ""; System.Uri uri = new System.Uri(URL); which means you can use the same methods, e.g.
string url = uri.AbsoluteUri; // string host = uri.host // localhost Regex way:
Getting parts of a URL (Regex)
1I guess its enough to return absolute path..
Path.GetFileName( Request.Url.AbsolutePath ) using System.IO;
1