Getting the absolute path in ASP.NET (part 2)

In a previous blog, I received many comments on various ways I could get the application root.  I am thankful for such comments.  What most of the commenters did not realize is that I need the resolution of the url in a custom http module, where Control.ResolveUrl is not practical to use.  And since I am using vanity urls (phantom paths that have no mapping to physical files or pages), I needed the resolution without the use of any System.IO objects.

However, I did make revisions to my method, and here it is:


1: public static string ResolveUrl(string url) 
2: {
3: if (url==null) throw new ArgumentNullException("url", "url can not be null");
4: if (url.Length == 0) throw new ArgumentException("The url can not be an empty string", "url");
5:
6: // there is no ~ in the first character position, just return the url
7: if (url[0] != '~') return url;
8:
9: string applicationPath = HttpContext.Current.Request.ApplicationPath;
10:
11: // there is just the ~ in the URL, return the applicatonPath
12: if (url.Length == 1) return applicationPath;
13:
14: // assume url looks like ~somePage
15: int indexOfUrl=1;
16:
17: // determine the middle character
18: string midPath = (applicationPath.Length >1 )? "/" : string.Empty;
19:
20: // if url looks like ~/ or ~\ change the indexOfUrl to 2
21: if (url[1] == '/' || url[1] == '\\') indexOfUrl=2;
22:
23: return applicationPath + midPath + url.Substring(indexOfUrl);
24: }// ResolveUrl
Any comments?


 

 

Published Friday, June 18, 2004 10:41 PM by Palermo4
Filed under: , , ,

Comments

# re: Getting the absolute path in ASP.NET (part 2)

Saturday, June 19, 2004 2:58 AM by Jeff Perrin
You're keeping us in suspense, Palermo. ;) What's the method that you've revised?

# re: Getting the absolute path in ASP.NET (part 2)

Saturday, June 19, 2004 3:01 AM by J. Michael Palermo IV
It is a revision to my ResolveUrl method.

# re: Getting the absolute path in ASP.NET (part 2)

Saturday, June 19, 2004 3:31 AM by J. Michael Palermo IV
Although... In my previous blog the name of the method was GetPathFromRoot. Thus my revision was also in the name of the method as well. Sorry for the confusion.

# Purpose of Blog

Tuesday, July 24, 2007 11:47 AM by Palermo4

I have recently decided to redo tags on my blog . This effort required me to go back in the archives

# Purpose of Blog

Tuesday, July 24, 2007 12:05 PM by Community Blogs

I have recently decided to redo tags on my blog . This effort required me to go back in the archives

# re: Getting the absolute path in ASP.NET (part 2)

Saturday, April 05, 2008 1:33 PM by Allan McLemore

When you try to generate Urls using the various values built into ASP.NET (ApplicationPath, "~", etc), you will encounter problems in some scenarios.

Check out my post on this issue:  www.zenternal.com/weblog

The code sample provided in my article does not address the issue with ASP.NET returning localhost when running on XP Workstation.  However, the URL methodology in the code can be improved to use System.Environment.MachineName whenever Request.ServerVariables["SERVER_NAME"] returns localhost.  I'll try to post an update to the article soon.

Also, check out this post:

www.thejackol.com/.../get-application-path-c-aspnet

Leave a Comment

(required) 
(required) 
(optional)
(required)