Hi Adam,
I am familar with the issue you describe above. This is a particular issue when you have several environments, Development, Test, Production, etc... I have tried most of what everyone has suggested above, but have found them to be only partially effective. What I did to finally solve this issue was to create a custom control that I call an "AnchorDomain". This control simply allows you to put in a path such as "/category/page" or "/images/file.png" into its VirtulPath propery and it will dynamically build the correct url for you based on the environment in which you are running your project. This way for all the problem url in my project I simply use this control and am able to run my code independent of the environment and all of my url resolve with the correct name. So for instance here is an example of the syntax in a aspx page.
<cc1:AnchorDomain runat="server" Title="My Homepage" Url="default.aspx" UsePageVirtualPath="false" />
So in this case no matter what environment you are in this link will always show the correct address.
At first it might seem like overkill to do it this way, but this control solves this issue in all cases i have run across.
See code below:
Enjoy!
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Pdc.EventPro.WebControls
{
[DefaultProperty("Text"), ToolboxData("<{0}:AnchorDomain1 runat=server></{0}:AnchorDomain1>")]
public class AnchorDomain : Control
{
private string _href = string.Empty;
public AnchorDomain()
{
VirtualPath = HttpContext.Current.Request.Path.Substring(0, HttpContext.Current.Request.Path.LastIndexOf("/") + 1);
}
private string VirtualPath
{
get
{
return (string)ViewState["virtualPath"];
}
set
{
ViewState["virtualPath"] = value;
}
}
[Bindable(true), Category("Content"), DefaultValue("Performance Development Corporation")]
public string Title
{
get
{
return (string)ViewState["title"];
}
set
{
ViewState["title"] = value;
}
}
[Bindable(true), Category("Content"), DefaultValue("")]
public string LinkText
{
get
{
return (string)ViewState["linktext"];
}
set
{
ViewState["linktext"] = value;
}
}
[Bindable(true), Category("Content"), DefaultValue("")]
public string Url
{
get
{
return (string)ViewState["url"];
}
set
{
ViewState["url"] = value;
}
}
[Bindable(true), Category("Content"), DefaultValue("false")]
public bool UsePageVirtualPath
{
get
{
return (bool)ViewState["useVirtualPath"];
}
set
{
ViewState["useVirtualPath"] = value;
}
}
[Bindable(true), Category("Content"), DefaultValue("false")]
public string CssClass
{
get
{
return (string)ViewState["CssClass"];
}
set
{
ViewState["CssClass"] = value;
}
}
protected override void Render(HtmlTextWriter writer)
{
if (string.IsNullOrEmpty(Url) && UsePageVirtualPath == false)
{
_href = CreateUri(HttpContext.Current.Request.Url.GetLeftPart(System.UriPartial.Authority), HttpContext.Current.Request.ApplicationPath).ToString();
}
else if (!string.IsNullOrEmpty(Url) && UsePageVirtualPath == true)
{
_href = CreateUri(HttpContext.Current.Request.Url.GetLeftPart(System.UriPartial.Authority), CombineUri(VirtualPath, Url)).ToString();
}
else
{
_href = CreateUri(HttpContext.Current.Request.Url.GetLeftPart(System.UriPartial.Authority), CombineUri(HttpContext.Current.Request.ApplicationPath, Url)).ToString();
}
writer.WriteBeginTag("a");
writer.WriteAttribute("href", _href);
writer.WriteAttribute("title", Title);
writer.WriteAttribute("class", CssClass);
writer.Write(HtmlTextWriter.TagRightChar);
writer.Write(LinkText);
writer.WriteEndTag("a");
base.Render(writer);
}
private Uri CreateUri(string baseUri, string relativeUri)
{
Uri result = null;
if (Uri.TryCreate(new Uri(baseUri), relativeUri, out result))
{
return result;
}
return result;
}
private string CombineUri(string basePath1, string basePath2)
{
return string.Format("{0}/{1}", basePath1.TrimEnd('/'), basePath2.TrimStart('/'));
}
}
}