标记问题中的 AppSettings

发布于 2024-08-10 14:13:51 字数 213 浏览 6 评论 0原文

我试图将其放入我的标记中:

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

但由于某种原因,这不被接受。我在这里做错了什么?

要求是我不使用辅助方法,但在标记中使用表达式构建器。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄意 2024-08-17 14:13:51

根据文档,这是不允许的:

如果要在页面或控件上使用表达式作为静态值,则可以将表达式用作 ASP.NET 服务器控件的一部分。典型的策略是添加 Literal 控件并将其 Text 属性设置为表达式。例如,要将版权声明放置在每个页面的底部,您可以使用以下内容:

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

如果想在 aspx 文件中完成所有操作,这可能会帮助您:

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

注意文本变量中令人不快的单引号 - 尝试使用转义的双引号引号会导致“格式错误的脚本标记”错误。


编辑:抱歉 - 我已经交换了顺序,这确实有效。

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.

花辞树 2024-08-17 14:13:51

当我这样做时,我通常会创建一个帮助器类,我喜欢调用 Config 并在其中为相关的应用程序设置放置一个静态属性。

那么您的代码将变为:

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

这样做的其他一些好处是,如果我决定将 ProxyScriptUrl 移至不同的配置机制,我只需修改一个类。您的配置类可能如下所示:

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

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 "];
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文