posted on Monday, August 15, 2005 3:16 PM by richardSlade

Configure your site to drop anywhere

I've often found that when I build a web app, I need a standard way of recalling certain pages/controls by the full URL.

I also don't want my pages littered with mappath statements. I need to be able to account for what happens when I drop my web application onto another server that has, for example, SSL certificates installed, or perhaps we need to put the application in a directory off the main site.

Well, I think that this way of acheiving this seems to be easy to configure and provides a standard means for any .NET site. Here's what I do...

A small web.config entry

Simply add the following line to the web.config file. This is the only thing that you may ever need to change when you place your site in a different server/folder.

A small method

Add this simple method to a class in your project

public string ProjectUrl()
{
string httpType = "http://";
string baseUrl = HttpContext.Current.Request.Url.Authority;
string projectFolder = ConfigurationSettings.AppSettings["projectfolder"];
if (HttpContext.Current.Request.IsSecureConnection)
{
httpType = "https://";
}
return httpType + baseUrl + projectFolder;
}

Using it

This can be used in all sorts of places. References in user controls, the global.asax for exmaple. If your web directory changes (when publishing from dev - production server for exmaple), just change the web.config entry. Https is already taken care of. Of couse, when you call the method you can just apend whatever page, image, etc.. that you would like on the end.

Cheers R

Comments