I've got a project where I'm tasked with changing hard-coded domain references from one domain to another in a group of legacy C#/VB web projects. I want to parameterize the domains as much as possible instead of just replacing with a different hard-coded value. The problem is that there are over 800 of these references in about 30 different solutions, so creating variables in each code-behind to bind to would take forever.
I have added the new domains to the appSettings section of the web.config file, and this works:
<asp:HyperLink Text="Link" NavigateUrl="<%$appSettings:DomainX %>" runat="server" />
But I need to be able to do something like this:
<asp:HyperLink Text="Link" NavigateUrl="<%$appSettings:DomainX %>/newPage.aspx" runat="server" />
But when I add the "/newPage.aspx" the page doesn't compile anymore. I don't really care if this is done with an asp:HyperLink tag or just a tag.
Any ideas on how I can accomplish this?
Thanks.
I think you have two options. The easiest is to just use a plain old anchor tag, if you're not doing anything with the HyperLink
server side:
<a href="<%= string.Concat(ConfigurationManager.AppSettings["DomainX"], "/newPage.aspx") %>">Link</a>
Alternatively, you could set the NavigateUrl
in the Page_Load
, since <%= will not work properly within the HyperLink
server tag:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
link1.NavigateUrl = string.Concat("http://",
ConfigurationManager.AppSettings["DomainX"], "/newPage.aspx");
}
You could also see if you can make a custom binding, something like $myBinding:DomainX
, but I don't know if that's possible off the top of my head (I would assume it is though).
EDIT
That $appSettings:DomainX
code is called an ASP.NET Expression, and can you can create custom expressions. This post from Phil Haack covers how to set them up, in case you are interested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With