Wednesday, 14 October 2009

An easy way to keep your dev and live server urls in sync

A common setup for your live server is to run your website on the root of the domain. By this I mean if you wanted to go to the homepage of your site you would type in

http://www.example.com/

When you are working on your dev copy of the site with Cassini on localhost (the built in dev web server) though you find your urls look more like

http://localhost:4865/YourProjectName/

If you're working purely with asp.net components in your site then you can always rely on the squiggly notation (called the tilde) to ensure your urls are mapped to what is called your "Application Root".

So you could have an <asp:Image> that has an ImageUrl attribute which looks like this:

<asp:Image runat="server" ID="Image1" ImageUrl="~/Images/thumb1.gif" />

And this would get turned into the proper url(either /Images/thumb1.gif or /YourProjectName/Images/thumb1.gif).

The trouble is that in most projects you need to use some links that don't mesh into the tilde enabled mapping systems such as the urls inside your CSS files - or even linking the CSS files into your template.

If all this is sounding like a familiar problem then this tip is for you. The trick we need to perform is to make Cassini run your dev website on the root of the localhost domain rather than in a subdirectory. That way a single url can be used on both your dev and live servers.

The steps are pretty simple:

  1. In the Solution Explorer window you select your Project node
  2. In the Properties window (press F4 if you can't see it) you change the "Virtual path" attribute from /ProjectName to /

The two windows look like this in Visual Studio 2008:

how-to-change-site-root

Shout it kick it on DotNetKicks.com

2 comments:

Kyle said...

Check out this article by ScottGu. There is no need to change anything between your dev and live environments if you follow this setup:

http://weblogs.asp.net/scottgu/archive/2006/12/19/tip-trick-how-to-run-a-root-site-with-the-local-web-server-using-vs-2005-sp1.aspx

rtpHarry said...

Thanks for your comment Kyle. Scott is suggesting the same tip that I am giving - except he beat me by about three years :)