729

Can anyone help out me in getting the URL of the current working page of ASP.NET in C#?

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 19
    @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 Allen Feb 12 '14 at 00: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. – Gordon Feb 12 '14 at 07:00
  • 9
    @Gordon Sorry if the caps offended you. It would be nice if dupes could be exposed in a single merged page. – Mark Allen Feb 12 '14 at 20:45
  • 5
    Seems 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... – Liath Jan 08 '15 at 16:04
  • I agree - it would be nice if there were a way to merge duplicate questions into a single question... – Taraz Jul 24 '18 at 18:43

9 Answers9

984

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
Canavar
  • 47,715
  • 17
  • 91
  • 122
  • 5
    AbsolutePath appears to return "/" in .NET 3.5 (haven't tested other versions) for paths similar to twitter user accounts, such as http://twitter.com/#!/user. You can use the Fragment method to get anything after the pound (#). – Ben Pearson Aug 05 '11 at 12:11
  • 3
    Actually 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 – Rody Apr 20 '12 at 12:18
  • what do you do if `HttpContext.Current` is null? – drzaus May 07 '12 at 18:50
  • 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. – Trisped May 23 '12 at 03:19
  • 2
    If 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 – dvdmn May 23 '13 at 14:56
  • What if i have '#' in URL..... i.e :http:test.abc.com/sitesposure.aspx#commentfocus......... will it work? – Pranav Bilurkar Jan 27 '15 at 14:56
  • @Canavar I am using url rewriting. how can I get the user-friendly path? – user786 Aug 18 '15 at 05:03
  • Because it isn't clear from this answer, `HttpContext.Current.Request.Url.Host` also includes the `.com/.org/.co`, etc. – James G. Nov 22 '16 at 17:30
  • @PB-BitWiser `HttpContext.Current.Request.Url.AbsoluteUri` would grab it all, including your `#` argument. – vapcguy Dec 04 '18 at 16:06
  • note that you have to put System.Web in front of this to access the correct HttpContext from a controller. – John Lord Jul 05 '19 at 13:32
  • Sometimes you don't even have to write `HttpContext.Current`. In razor pages for exemple. Just `Request.Url.AbsolutePath` worked for me. – Antoine Pelletier Oct 04 '19 at 17:42
555

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

Learning
  • 19,469
  • 39
  • 180
  • 373
111

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/"
Soenhay
  • 3,958
  • 5
  • 34
  • 60
  • 1
    Just to state the obvious: HttpContext is located in System.Web, i.e System.Web.HttpContext. msdn: http://msdn.microsoft.com/en-us/library/system.web.httpcontext.aspx – Sindre Jul 06 '12 at 09:17
  • 20
    Even simpler is: `Request.Url.GetLeftPart(UriPartial.Authority)` See here: https://msdn.microsoft.com/en-us/library/system.uri.getleftpart(v=vs.110).aspx – Rosdi Kasim Aug 09 '15 at 18:51
  • @rosdikasim thank you .This i looking for.Request.Url.GetLeftPart(UriPartial.Authority) ===>https://localhost:44373 – lava Nov 09 '20 at 12:56
  • a complete command to get https://www.something.com HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) – Ariwibawa Jan 28 '21 at 05:43
50

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

roman m
  • 26,012
  • 31
  • 101
  • 133
25

the request.rawurl will gives the content of current page it gives the exact path that you required

use HttpContext.Current.Request.RawUrl

Mikael Engver
  • 4,634
  • 4
  • 46
  • 53
Randhi Rupesh
  • 14,650
  • 9
  • 27
  • 46
14

If you want to get

localhost:2806 

from

http://localhost:2806/Pages/ 

then use:

HttpContext.Current.Request.Url.Authority
Thomas Amar
  • 352
  • 10
  • 20
12

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..

dvdmn
  • 6,456
  • 7
  • 44
  • 52
9

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:

Getting parts of a URL (Regex)

Community
  • 1
  • 1
Ben Pearson
  • 7,532
  • 4
  • 30
  • 50
  • 1
    Also 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. – Ben Pearson Aug 05 '11 at 11:21
6

I guess its enough to return absolute path..

 Path.GetFileName( Request.Url.AbsolutePath )

using System.IO;

j.rmz87
  • 786
  • 1
  • 7
  • 18
Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67