726

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

5
  • 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 Allen
    Feb 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.
    – Gordon
    Feb 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 Allen
    Feb 12, 2014 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 8, 2015 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, 2018 at 18:43

9 Answers 9

979

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
11
  • 5
    AbsolutePath 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
  • 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, 2012 at 12:18
  • what do you do if HttpContext.Current is null?
    – drzaus
    May 7, 2012 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, 2012 at 3: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, 2013 at 14:56
552

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

2
  • Among first 2 which one is secure way? Mar 9, 2020 at 13:13
  • this is the best answer for sure!
    – Alexander
    Oct 8, 2020 at 14:24
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/"
4
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

3
  • 13
    Does not include port. Need: HttpContext.Current.Request.Url.Port.
    – JohnB
    Apr 8, 2012 at 0:53
  • 3
    @JohnB or rather HttpContext.Current.Request.Url.Authority, fetching host:port with one call...
    – T_D
    Jun 15, 2017 at 14:20
  • As @JohnB says, Authority is safer than Host if the port is different than 80 or 443.
    – vapcguy
    Dec 4, 2018 at 16:09
25

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

use HttpContext.Current.Request.RawUrl

14

If you want to get

localhost:2806 

from

http://localhost:2806/Pages/ 

then use:

HttpContext.Current.Request.Url.Authority
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..

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)

1
  • 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. Aug 5, 2011 at 11:21
6

I guess its enough to return absolute path..

 Path.GetFileName( Request.Url.AbsolutePath )

using System.IO;

1
  • 1
    I don't think that would satisfy OP because it only gives the page name :(
    – clamchoda
    Nov 5, 2012 at 16:39