Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

Don't forget about the URI class


:P
On this page:
The URI class is one that I often forget about. In fact, I got an email from Scott Mitchell today asking about some alternate code to establish a 'base path'.

Sometimes you need to find out a base path in order to use it inside of a page so that images and related links can load properly. Oddly enough you can get the application path but it's not a full URL, only an application relative path.

The base path in this scenario would be:

Original Url
http://www.west-wind.com/wwStore/admin/default.aspx

Base Path
http://www.west-wind.com/wwStore

To get this I used code like this:

string Port = Request.ServerVariables["SERVER_PORT"]; if (Port == null || Port == "80" || Port == "443") Port = ""; else Port = ":" + Port; string Protocol = Request.ServerVariables["SERVER_PORT_SECURE"]; if (Protocol == null || Protocol == "0") Protocol = "http://"; else Protocol = "https://"; // *** Figure out the base Url which points at the application's root this.BasePath = Protocol + Request.ServerVariables["SERVER_NAME"] + Port + Request.ApplicationPath;

While this works, manual URL parsing is pretty slow and cumbersome. The shortcut to this is to use the URI class from Request.Url:

this.BasePath = Request.Url.GetLeftPart( UriPartial.Authority ) + this.ResolveUrl( Request.ApplicationPath) ;

It pays to remember the URI class - shorter code and more reliable parsing of URLs from the framework.


The Voices of Reason


 

Scott Mitchell
February 01, 2005

# re: Don't forget about the URI class

To give credit where credit is due, Atif Aziz is the one who pointed out to me that Request.Url.GetLeftPart( UriPartial.Authority ) could be used...

Atif Aziz
February 01, 2005

# re: Don't forget about the URI class

If you're appending the ApplicationPath, then you'd also want to trim a trailing slash to accommodate for cases where the application is installed at the root of the web. So the small tweak needed at the end is:

this.BasePath = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath.TrimEnd('/');

Rick Strahl
February 01, 2005

# re: Don't forget about the URI class

Thanks Scott and Atif! I actually use the above quite frequently in my apps (especially in relation to a generic message/error page) so this has come in handy.

Randy Pearson
February 08, 2005

# re: Don't forget about the URI class

Why is "/admin" omitted from the base path. Seems like without that, relative HREF values wouldn't work--would be off by one level.

Rick Strahl
February 08, 2005

# re: Don't forget about the URI class

You can get that portion of the path seperately with Page.TemplateSourceDirectory. The key here was to have an easy way to retrieve the applications base path from which you CAN always create a good complete path. In most applications, you'll often get screwed by a relative path with generic pages if you move around directories. A good example is the MessageDisplay class I talk about here (which is where this issue popped up in the first place):

http://www.west-wind.com/presentations/wwMessageDisplay/wwMessageDisplay.asp

More info on all the path options are here:
http://west-wind.com/weblog/posts/269.aspx

Rick Strahl's Web Log
October 15, 2006

# Making sense of ASP.Net Paths - Rick Strahl's Web Log

ASP.Net a plethora of properties to retrieve path information about the current request, control and application. To keep things straight I thought it'd be a good idea to summarize those options briefly along with describing some common scenarios of how they might be used.

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024