AppSettings in markup issue

余生长醉 提交于 2019-12-11 02:12:12

问题


I am trying to put this in my markup:

<script type="text/javascript" src="<%$ AppSettings:proxyScriptUrl %>"></script>

But for some reason this is not accepted. What am I doing wrong here?

The requirement is that I do not use a helper method but that the expressionbuilder is used in the markup.


回答1:


According to the documentation, that's not allowed:

If you want to use an expression as a static value on your page or control, you use an expression as part of an ASP.NET server control. A typical strategy is to add a Literal control and set its Text property to an expression. For example, to place a copyright notice at the bottom of every page you could use the following:

<p align="center">
  <asp:Literal runat="server" text="<%$ AppSettings: copyright %>"/>
</p>

This might help you if want to do it all in the aspx file:

<script type='text/javascript' src='<asp:Literal id="literal1" runat="server" text="<%$ AppSettings: jsSource %>" />'></script>

Note the unpleasant single quotes in the text variable - trying to us escaped double quotes results in "Badly formed script tag" errors.


Edit: apologies - I've swapped the order around this does work.




回答2:


When I do this I usually create a helper class I like to call Config and put a static property on there for the App Settings in question.

Then your code would become:

<script type="text/javascript" src="<%=Config.ProxyScriptUrl%>"/>

Some of the other benefits of this is that if I decide to move the ProxyScriptUrl to a different configuration mechanism I only have to modify the one class. Your config class might look like:

public static class Config
{
    public static string ProxyScriptUrl 
    {
        get
        {
            return WebConfigurationManager.AppSettings["proxyScriptUrl "];
        }
    }
}


来源:https://stackoverflow.com/questions/1681892/appsettings-in-markup-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!