Click here to Skip to main content
15,438,963 members
Articles / Web Development / ASP.NET
Posted 20 Jan 2010

Tagged as


70 bookmarked

ResolveUrl in ASP.NET - The Perfect Solution

Rate me:
Please Sign up or sign in to vote.
4.88/5 (38 votes)
26 Jan 2010CPOL2 min read
If you are looking for ResolveUrl outside of Page/Control, and even if you are not, this is for you.


From my personal experience using ASP.NET, and from searching the web, I have found that the ResolveUrl method of the Page control (and basically, Control) presents us with some serious problems.

The most common one is that you just cannot use it outside of the page or control context.

Other problems are just bugs. It will not correctly handle some of the URLs you'll give it. For example, try Page.ResolveUrl("~/test.aspx?param="). The result is the very same input string... It will just do nothing. By looking into the ASP.NET code using Reflector, I found that all mechanisms that are supposed to convert the relative URLs to absolute URLs will search first for a "://" inside the string, and will return if found. So a query string is OK, unless you pass in a parameter with ://. Yes, I know that the query string parameter should be UrlEncoded, but if it isn't, it is still an acceptable URL. Seriously, check your browsers!

Other suggested methods on the web involve using VirtualPathUtility.ToAbsolute, which is pretty nice and handy, unless you pass in a query string with the URL... Because it will just throw an exception. It will also throw an exception for an absolute URL!

So I decided to find the ultimate solution.

Using the Code

First, I searched for the perfect variable that will give me the Application Virtual Path at runtime without a page context.

I found this to be HttpRuntime.AppDomainAppVirtualPath. It will work anywhere - even inside a timer callback! It gives the path without a trailing slash (ASP.NET makes a special effort to remove the trailing slash...), but that is OK, we can fix it :-)

Then, I did some tests on the original ResolveUrl code, and found where I need to replace what with the AppVirtualPath:

  1. When the URL begins with a slash (either / or \), it will not touch it!
  2. When the URL begins with ~/, it will replace it with the AppVirtualPath.
  3. When the URL is an absolute URL, it will not touch it. (ResolveUrl has a bug with this, as I said before...)
  4. In any other case (even beginning with ~, but not slash), it will append the URL to the AppVirtualPath.
  5. Whenever it modifies the URL, it also fixes up the slashes. Removes double slashes and replaces \ with /.

So I replicated all of that, but without the bugs. And here's the code:

public static string ResolveUrl(string relativeUrl)
    if (relativeUrl == null) throw new ArgumentNullException("relativeUrl");
    if (relativeUrl.Length == 0 || relativeUrl[0] == '/' || relativeUrl[0] == '\\') 
        return relativeUrl;
    int idxOfScheme = relativeUrl.IndexOf(@"://", StringComparison.Ordinal);
    if (idxOfScheme != -1)
        int idxOfQM = relativeUrl.IndexOf('?');
        if (idxOfQM == -1 || idxOfQM > idxOfScheme) return relativeUrl;
    StringBuilder sbUrl = new StringBuilder();
    if (sbUrl.Length == 0 || sbUrl[sbUrl.Length - 1] != '/') sbUrl.Append('/');
    // found question mark already? query string, do not touch!
    bool foundQM = false;
    bool foundSlash; // the latest char was a slash?
    if (relativeUrl.Length > 1
        && relativeUrl[0] == '~'
        && (relativeUrl[1] == '/' || relativeUrl[1] == '\\'))
        relativeUrl = relativeUrl.Substring(2);
        foundSlash = true;
    else foundSlash = false;
    foreach (char c in relativeUrl)
        if (!foundQM)
            if (c == '?') foundQM = true;
                if (c == '/' || c == '\\')
                    if (foundSlash) continue;
                        foundSlash = true;
                else if (foundSlash) foundSlash = false;
    return sbUrl.ToString();

Points of Interest

After completing the code and testing over and over again and comparing to the original ResolveUrl, I started to test for performance... In most cases, my code executed faster than the original ResolveUrl by 2.7 times! I also tested inside loops that executed the code 100000s of times on different kinds of URLs.


This is the first version, and hopefully the last!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionMy vote 5 (Y) Pin
Sameh Saeed Mohammad16-Oct-14 1:21
professionalSameh Saeed Mohammad16-Oct-14 1:21 
QuestionOr why not just use Pin
frno684-Sep-14 21:59
Memberfrno684-Sep-14 21:59 
AnswerRe: Or why not just use Pin
Daniel Cohen Gindi4-Sep-14 22:10
MemberDaniel Cohen Gindi4-Sep-14 22:10 
BugResolved on full url removed a forwardslash Pin
brother.gabriel17-Oct-13 16:39
Memberbrother.gabriel17-Oct-13 16:39 
GeneralRe: Resolved on full url removed a forwardslash Pin
Daniel Cohen Gindi4-Sep-14 22:09
MemberDaniel Cohen Gindi4-Sep-14 22:09 
GeneralMy vote of 5 Pin
Naveenlplp20-Aug-13 23:49
MemberNaveenlplp20-Aug-13 23:49 
QuestionSystem.Web.VirtualPathUtility? Pin
KiwiPiet11-Oct-12 15:51
MemberKiwiPiet11-Oct-12 15:51 
AnswerRe: System.Web.VirtualPathUtility? Pin
Daniel Cohen Gindi11-Oct-12 20:31
MemberDaniel Cohen Gindi11-Oct-12 20:31 
GeneralJust what I came here for... Pin
Saul Johnson4-Aug-12 10:56
MemberSaul Johnson4-Aug-12 10:56 
GeneralRe: Just what I came here for... Pin
Daniel Cohen Gindi4-Aug-12 10:57
MemberDaniel Cohen Gindi4-Aug-12 10:57 
SuggestionA suggestion to make it even better Pin
Robbie Couret9-Nov-11 9:55
MemberRobbie Couret9-Nov-11 9:55 
GeneralMy vote of 5 Pin
Anurag Gandhi4-Aug-11 20:46
professionalAnurag Gandhi4-Aug-11 20:46 
GeneralGreat job! Pin
DotNetWise8-Dec-10 12:23
MemberDotNetWise8-Dec-10 12:23 
Dude, you rocked my day! Laugh | :laugh:
GeneralMy vote of 5 Pin
ToeStumper1-Sep-10 6:02
MemberToeStumper1-Sep-10 6:02 
GeneralRe: My vote of 5 Pin
Daniel Cohen Gindi1-Sep-10 10:02
MemberDaniel Cohen Gindi1-Sep-10 10:02 
GeneralGreat job! Pin
ToeStumper1-Sep-10 5:59
MemberToeStumper1-Sep-10 5:59 
GeneralRe: Great job! Pin
Daniel Cohen Gindi1-Sep-10 9:56
MemberDaniel Cohen Gindi1-Sep-10 9:56 
GeneralMy vote of 5 Pin
Adam Maras26-Jan-10 10:54
MemberAdam Maras26-Jan-10 10:54 
GeneralRe: My vote of 5 Pin
Daniel Cohen Gindi9-Apr-10 2:42
MemberDaniel Cohen Gindi9-Apr-10 2:42 
General[My Vote of 5] Pin
Chris Carter26-Jan-10 5:25
MemberChris Carter26-Jan-10 5:25 
GeneralRe: [My Vote of 5] Pin
Daniel Cohen Gindi26-Jan-10 5:28
MemberDaniel Cohen Gindi26-Jan-10 5:28 
General[My vote of 1] Why hack .net find a real solution Pin
RenderEngSol26-Jan-10 0:44
MemberRenderEngSol26-Jan-10 0:44 
GeneralRe: [My vote of 1] Why hack .net find a real solution Pin
Daniel Cohen Gindi26-Jan-10 0:53
MemberDaniel Cohen Gindi26-Jan-10 0:53 
GeneralRe: [My vote of 1] Why hack .net find a real solution Pin
RenderEngSol26-Jan-10 1:14
MemberRenderEngSol26-Jan-10 1:14 
GeneralRe: [My vote of 1] Why hack .net find a real solution Pin
Daniel Cohen Gindi26-Jan-10 1:21
MemberDaniel Cohen Gindi26-Jan-10 1:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.