Can anyone help out me in getting the URL of the current working page of ASP.NET in C#?
-
18@Gordon So which is it? You marked this question as being the duplicate... OF A QUESTION THAT YOU MARKED AS BEING A DUPLICATE TOO, ONE MINUTE LATER. Maybe we need a feature request to get StackOverflow to add "no follow" to marked duplicates, because all I wanted was an answer and the search engine brought me here first. I won't make such a request however, lest it be marked duplicate. :(– Mark AllenFeb 12, 2014 at 0:57
-
3@MarkAllen not sure why you are yelling at me? if you follow the dupes, you'll end up at the oldest question (i could find then) asking this. Which of it is it? Well, all of them. It doesn't matter where you ended up searching. This page gives you 14 answers, the closed against one gives you 15 and the original one gives you another 11. If I wouldn't close them questions, you'd only get 14, not 40. Note that you have less than 10k rep so you dont see the deleted ones.– GordonFeb 12, 2014 at 7:00
-
9@Gordon Sorry if the caps offended you. It would be nice if dupes could be exposed in a single merged page.– Mark AllenFeb 12, 2014 at 20:45
-
5Seems to me that the primary should be the one which is either the best asked (highest vote) or the one with the best answer. Often that will be the oldest but not always. Maybe a conversion for meta...– LiathJan 8, 2015 at 16:04
-
I agree - it would be nice if there were a way to merge duplicate questions into a single question...– TarazJul 24, 2018 at 18:43
9 Answers
Try this :
string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx
string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TESTERS/Default6.aspx
string host = HttpContext.Current.Request.Url.Host;
// localhost
-
5AbsolutePath appears to return "/" in .NET 3.5 (haven't tested other versions) for paths similar to twitter user accounts, such as twitter.com/#!/user. You can use the Fragment method to get anything after the pound (#). Aug 5, 2011 at 12:11
-
3Actually these are not correct when used with a rewriting mechanism like UrlRewriter.net because this will give the rewritten URL. You can then use: Request.Url.GetLeftPart(UriPartial.Authority) + Request.RawUrl– RodyApr 20, 2012 at 12:18
-
-
8@drzaus if
HttpContext.Current
is null then you are not processing a page request or you are attempting to access it before it is set. If you need additional help please start a new question.– TrispedMay 23, 2012 at 3:19 -
2If you need to run this in global.asax > Application_Start and you app pool mode is "integrated" then you will receive "Request is not available in this context exception in Application_Start" error. In that case you need to use System.Web.HttpRuntime.AppDomainAppVirtualPath– dvdmnMay 23, 2013 at 14:56
You may at times need to get different values from URL.
Below example shows different ways of extracting different parts of URL
EXAMPLE: (Sample URL)
http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2
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: http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString1=2
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.
http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx
Just sharing as this was my solution thanks to Canavar's post.
If you have something like this:
"http://localhost:1234/Default.aspx?un=asdf&somethingelse=fdsa"
or like this:
"https://www.something.com/index.html?a=123&b=4567"
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:
"http://localhost:1234/"
"https://www.something.com/"
-
1Just to state the obvious: HttpContext is located in System.Web, i.e System.Web.HttpContext. msdn: msdn.microsoft.com/en-us/library/system.web.httpcontext.aspx– SindreJul 6, 2012 at 9:17
-
20Even simpler is:
Request.Url.GetLeftPart(UriPartial.Authority)
See here: msdn.microsoft.com/en-us/library/… Aug 9, 2015 at 18:51 -
@rosdikasim thank you .This i looking for.Request.Url.GetLeftPart(UriPartial.Authority) ===>localhost:44373– lavaNov 9, 2020 at 12:56
-
a complete command to get something.com HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) Jan 28, 2021 at 5:43
if 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
the 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
http://localhost:2806/Pages/
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 = "http://localhost:1302/TESTERS/Default6.aspx";
System.Uri uri = new System.Uri(URL);
which means you can use the same methods, e.g.
string url = uri.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx
string host = uri.host
// localhost
Regex way:
-
1Also worth noting that if the string can not be parsed it will throw a System.FormatException. However, the System.Uri.TryCreate method was introduced in .NET 4. Aug 5, 2011 at 11:21
I guess its enough to return absolute path..
Path.GetFileName( Request.Url.AbsolutePath )
using System.IO;
-
1I don't think that would satisfy OP because it only gives the page name :( Nov 5, 2012 at 16:39