views:

52

answers:

3

I have a habit of hardcoding URLs into my HTML:

...<a href="www.mySite.com/login">logon to your account.</a>

During development when I want to target a specific web-app version I will global search/replace the 'www.mySite' with something like 'myDev.mySite'.

Since this practice has become habitual I can't clearly remember if there's a drop-dead good reason I don't use relative address or if i'm just that persistently dumb.

I would like to think that .net has something similar to the way we define connectionstrings that I could define a root URL as a global variable but so far haven't found the feature.

A: 

<base href> pretty much does exactly what you want.

http://www.w3schools.com/TAGS/tag%5Fbase.asp

recursive
+3  A: 

In ASP.NET MVC, use

<a href='<%=Url.Action("Login")%>'>login</a>

it will automatically generate the URL that works.

Jan Willem B
A: 

Jan's answer is the best for ASP.NET MVC, since you can independently change how URLs map to views. A more general solution for any ASP.NET site is by using the tilde. For example,

Page.ResolveClientUrl("~/My/Path.aspx")

Will automatically resolve the ~ to the web application root. Or if you use ASP.NET controls,

<asp:HyperLink runat="server" NavigateUrl="~/My/Path.aspx" Text="Link Text"/>

This will create a hyperlink with the path automatically resolved to the site root.

Jacob