I ran into a bit of a head scratcher today with a routine that does
some string manipulation. It's an old routine that I use to help me do
the equivalent of ResolveUrl() outside of the context of the ASP.NET
Page framework - typically in static code somewhere or as part of a
handler or other component.
The process is easy enough but I ran
into a snag with the special case of resolving a root web path (ie.
/wwbanner.ashx for example) and the routine has some admittedly hacky
code that checks for the possibility of duplicated slashes in the
front. So basically there's some code that looks like this:
newUrl = HttpContext.Current.Request.ApplicationPath +
originalUrl.Substring(1).Replace("//","/");
Now that seems reasonable to me and I could swear that this code should work.
However it doesn't. If I pass in ~/wwbanner.ashx with a root web the URL gets generated as:
//wwbanner.ashx
which oddly enough goes out and downloads a whole HTML page from
some PHP server off the Web (which is really quite a bizarre matter all
on its own).
Honestly I can't figure out though why the above code DOESN'T work.
The last Replace() at the end of the string expression should handle
the double forward slashes. If I explicitly put the Replace() portion
onto a separate line of code the code starts working properly:
newUrl = HttpContext.Current.Request.ApplicationPath +
originalUrl.Substring(1);
newUrl = newUrl.Replace("//", "/");
this properly produces:
/wwbanner.ashx
which works as expected and properly fires my local banner manager in the site.
Anybody see which part of the forest I'm missing here and why the
first fails and the latter works? It seems to me I SHOULD be able to
chain string commands together and get expectable results - in fact I'm
pretty sure I do this in other places and it works just fine. But
somehow in this scenario it seems that .Replace() in particular is not
behaving properly
Source:http://www.west-wind.com