Don't forget about the URI class
On this page:
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.
Other Posts you might also like
- Adding minimal OWIN Identity Authentication to an Existing ASP.NET MVC Application
- Resolving Paths To Server Relative Paths in .NET Code
- Map Physical Paths with an HttpContext.MapPath() Extension Method in ASP.NET
- Back to Basics: Rewriting a URL in ASP.NET Core
- Getting the Client IP Address in ASP.NET Core
The Voices of Reason
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('/');
this.BasePath = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath.TrimEnd('/');
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.
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
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
# re: Don't forget about the URI class