35

How can I get the application relative url from Request.Url.AbsolutePath?

VirtualPathUtility seems to only work with ~/XXX urls?

4
  • Hi, take a look here: stackoverflow.com/questions/3681052/… Commented Mar 14, 2012 at 11:52
  • @felipeoriani: That's relative path to absolute (opposite to what I want).
    – jgauffin
    Commented Mar 14, 2012 at 11:55
  • Is Request.ApplicationPath what you want ?
    – V4Vendetta
    Commented Mar 14, 2012 at 12:00
  • 1
    @PankajGarg Didn't get your point (if you made any) :)
    – V4Vendetta
    Commented Mar 14, 2012 at 17:27

7 Answers 7

37

Its a little late to answer but there is a elegant solution to this. You can use

Request.Url.PathAndQuery

This will return the relative path of the page.

For example, if the URL is www.example.com/products?A=a&B=b&C=c, the above piece of code will return /products?A=a&B=b&C=c

3
  • 10
    That's not a relative url. It's an absolute path from the root. Would not work for virtual directories.
    – jgauffin
    Commented Jul 31, 2012 at 6:49
  • 5
    But it's what many people need exactly when ending up here :)
    – Stefanvds
    Commented Nov 13, 2015 at 7:38
  • This doesn't include any anchor - which I wanted. Commented Mar 6, 2018 at 11:19
18

I solved it like this:

// create an absolute path for the application root
var appUrl = VirtualPathUtility.ToAbsolute("~/");

// remove the app path (exclude the last slash)
var relativeUrl = HttpContext.Current.Request.Url.AbsolutePath.Remove(0, appUrl.Length - 1);
2
  • if the HttpContext.Current.Request.Url.AbsolutePath Have a %20 for the Space what should i do ?
    – Marwan
    Commented Oct 22, 2013 at 9:40
  • Replace it with a space. Or use Uri.DecodeXXx() (do not remember the method name)
    – jgauffin
    Commented Oct 22, 2013 at 9:59
9

This worked for me:

VirtualPathUtility.MakeRelative("~", Request.Url.AbsolutePath)

For example if the root of the website is /Website and Request.Url.AbsolutePath is /Website/some/path this will return some/path.

9

To do this without using string manipulation and handling the application's relative path I used:

    var relativeUrl = VirtualPathUtility.ToAppRelative(
        new Uri(context.Request.Url.PathAndQuery, UriKind.Relative).ToString());
1
  • 1
    Why the new Uri(context.Request.Url.PathAndQuery, UriKind.Relative).ToString()? Isn't just context.Request.Url.PathAndQuery the same thing?
    – MiMo
    Commented Dec 15, 2016 at 21:27
4

I think this is the cleanest way:

new Uri(Request.Url.PathAndQuery, UriKind.Relative))
0
1
 String appUrl = VirtualPathUtility.ToAbsolute("~/");
 String RelativePath = new System.Uri(Page.Request.Url, "").PathAndQuery.Substring(appUrl.Length-1)
3
  • Suppose I have a URL http://localhost:49484/Website/Default.aspx This will give you /Default.aspx
    – Pankaj
    Commented Mar 14, 2012 at 14:31
  • You are doing the same thing as I did in my answer, but slightly more complex?
    – jgauffin
    Commented Mar 14, 2012 at 14:53
  • Difference is the class and approach and result are same with same line of code as you mentioned...
    – Pankaj
    Commented Mar 14, 2012 at 14:59
1

To build on Ashwin Singh's nice answer - I needed the anchor link to be included with my relative URL, so I just re-added it at the end:

Request.Url.PathAndQuery + Request.Url.Fragment

So http://localhost:8080/abc/d?foo=bar#jazz becomes /abc/d?foo=bar#jazz.

Not the answer you're looking for? Browse other questions tagged or ask your own question.