如何读取 HTML 标记中的 web.config APP 按键设置

发布于 2024-09-30 05:07:27 字数 267 浏览 1 评论 0原文

我有一个使用第 3 方 ActiveX 控件的 ASP.NET 站点。我必须将一些参数传递给 HTML 页面中的 OBJECT 标记。如果我将这些参数硬编码到 HTML 中,一切都会正常。

我想将参数与应用程序设置“键/值”对一起放置在我的 web.config 中。

我的问题是我无法读取 HTML 标记中的应用程序密钥设置以成功将它们作为参数传递。我可以从服务器端代码后面很好地阅读它们。

在客户端 HTML 标记中读取这些设置的正确方法是什么?

谢谢

I have an ASP.NET site which uses a 3rd party activeX control. I have to pass a few parameters to the OBJECT tag in the HTML page. If i hardcode these parameters into the HTML everything works.

I would like to place the parameters in my web.config with app settings "key/value" pairs.

My problem is i cannot read the app key setting in the HTML markup to succesfully pass them in as parameters. I can read them fine from server side code behind.

What's the correct way to read these settings in the client side HTML markup ?

Thanks

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

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

发布评论

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

评论(5

倦话 2024-10-07 05:07:28

除了使用 <%=ConfigurationManager.AppSettings["MyAttribute"]%>(正如其他人指出的那样)之外,您还可以使用 表达式构建器。语法有点不同。您可以使用 <%$ AppSettings: MyAttribute %> 而不是 <%=...%>,如下所示:

<object id="myObjectID attr="<%$ AppSettings: MyAttribute %>" ...>

如果您只是转储 appSettings 值直接转换为静态 HTML(正如我假设您在本示例中一样),这两种方法对于所有实际目的都是相同的。

不过,表达式生成器的好处在于,您可以使用它们以声明方式将 appSettings 值分配给 Web 控件属性,而使用 <%=...%> 则无法做到这一点; 语法。也就是说,使用表达式构建器,您可以执行以下操作:

<asp:Label runat="server" ... Text="<%$ AppSettings: MyAttribute %>" />

而您可以执行以下操作:

<asp:Label runat="server" ... Text="<%=ConfigurationManager.AppSettings["MyAttribute"]%>" />

In addition to using <%=ConfigurationManager.AppSettings["MyAttribute"]%>, as others have noted, you can also use expression builders. The syntax is a little different. Instead of <%=...%> you use <%$ AppSettings: MyAttribute %>, like so:

<object id="myObjectID attr="<%$ AppSettings: MyAttribute %>" ...>

If you are just dumping an appSettings value directly into static HTML (as I presume you are in this example), these two approaches are identical for all practical purposes.

What is nice about expression builders, though, is that you can use them to declaratively assign appSettings values to Web control properties, something you cannot do with the <%=...%> syntax. That is, with expression builders you can do something like:

<asp:Label runat="server" ... Text="<%$ AppSettings: MyAttribute %>" />

Whereas you could not do:

<asp:Label runat="server" ... Text="<%=ConfigurationManager.AppSettings["MyAttribute"]%>" />
她如夕阳 2024-10-07 05:07:28

以下代码:

<%$ AppSettings: MyAttribute %>

不兼容一般的 HTML 标记和 JavaScript 功能!这对 asp 标签很有用。

<%=ConfigurationManager.AppSettings("MyAttribute")%>

在一般 HTML 标记中确实有效。

我的建议也是如此

<%=ConfigurationManager.AppSettings("MyAttribute")%>

The following code:

<%$ AppSettings: MyAttribute %>

is not compatible with general HTML markup and JavaScript function! It's good for asp tag.

Whereas

<%=ConfigurationManager.AppSettings("MyAttribute")%>

really work in general HTML markup.

so

<%=ConfigurationManager.AppSettings("MyAttribute")%>

is my recommendation!

最美的太阳 2024-10-07 05:07:28

您可以在 ASPX 页面中使用 ConfigurationManager。然后您可以在 OBJECT 标记参数中添加:

Web.Config

</configuration>
    <appSettings>
        <add key="Setting" value="Value"/>
    <appSettings>
</configuration>

ASPX

<object>
    <param name="Setting" value="<%= System.Configuration.ConfigurationManager.AppSettings["Setting"] %>" />
</object>

You can use the ConfigurationManager in you ASPX page. Then you can add in your OBJECT tag parameters:

Web.Config

</configuration>
    <appSettings>
        <add key="Setting" value="Value"/>
    <appSettings>
</configuration>

ASPX

<object>
    <param name="Setting" value="<%= System.Configuration.ConfigurationManager.AppSettings["Setting"] %>" />
</object>
一场春暖 2024-10-07 05:07:28

我建议您在运行时从服务器动态生成 OBJECT 标记。这样您就可以注入从 web.config 文件中读取的任何参数。

I suggest you generate your OBJECT tag dynamically at run-time from the server. This way you can inject whatever parameters you read from the web.config file.

梦言归人 2024-10-07 05:07:28

你有几个选择。如果将 runat="server" 属性添加到对象标记,则可以使用其 ID 从代码隐藏中访问它,并以这种方式添加属性:

myObjectID.Attributes.Add("attrName", "value")

如果您不想这样做,则可以可以使用内联文字:

<object id="myObjectID attr="<%= ConfigurationManager.AppSettings("MyAttribute") %>" ...>

无论哪种方式都应该完成工作。

You have a few options. If you add the runat="server" attribute to your object tag, you can access it from your codebehind using its ID, and add attributes that way:

myObjectID.Attributes.Add("attrName", "value")

If you don't want to do that, you could use inline literals:

<object id="myObjectID attr="<%= ConfigurationManager.AppSettings("MyAttribute") %>" ...>

Either way should get the job done.

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