简体   繁体   中英

ASP.NET no evaluation of C# code in site.master page

I have an ASP.NET application and I would like to have a different logo depending on the environment (production, quality, etc...) so in my Site.Master page I have an image like this:

<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/logo_<%= EnvironmentHelper.GetRunningEnvironment() %>.png" AlternateText="logo"/>

The code behind is this:

    public static class EnvironmentHelper
{ public static string GetRunningEnvironment()
    {
        return string.IsNullOrEmpty(ConfigurationManager.AppSettings["RunningEnvironment"]) 
            ? "dev"
            : ConfigurationManager.AppSettings["RunningEnvironment"];
    }}

I have added in the Site.Master page, the namespace so the method can be called:

<%@ Import Namespace="Business.Helpers" %>

In the Web.config file, I created a variable in appSettings section like this:

<add key="RunningEnvironment" value="dev"/>

I have several images in my images folder with all the environments (logo_prod.png, logo_dev.png, etc....)

The problem is that the logo doesn't get displayed and the url of the picture is:

http://localhost/myapp/Images/logo_%3C%25=%20EnvironmentHelper.GetRunningEnvironment()%20%25%3E.png

why does the GetRunningEnvironment method is not evaluated ?

You are not allowed to use server tags inside that field.

You can simply fix this by either setting the URL from your code behind:

protected void Page_Load(object sender, EventArgs e)
{
    this.Image1.ImageUrl = "~/Images/logo_" + EnvironmentHelper.GetRunningEnvironment() + ".png";
}

or by using an HTML img element directly:

<img src="<%= "~/Images/logo_" + EnvironmentHelper.GetRunningEnvironment() + ".png" %>" alt="logo" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM