Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing app setting from aspx and add concatenated text

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.

like image 389
Mike Pennington Avatar asked Feb 24 '23 03:02

Mike Pennington


1 Answers

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.

like image 90
rsbarro Avatar answered Mar 24 '23 08:03

rsbarro