如何在 HTML 标记中读取 web.config APP 键设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4056827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read web.config APP key settings in HTML markup
提问by Khalid Rahaman
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.
我有一个使用 3rd 方 activeX 控件的 ASP.NET 站点。我必须将一些参数传递给 HTML 页面中的 OBJECT 标记。如果我将这些参数硬编码到 HTML 中,则一切正常。
I would like to place the parameters in my web.config with app settings "key/value" pairs.
我想使用应用程序设置“键/值”对将参数放在我的 web.config 中。
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.
我的问题是我无法读取 HTML 标记中的应用程序键设置以成功将它们作为参数传递。我可以从后面的服务器端代码中很好地读取它们。
What's the correct way to read these settings in the client side HTML markup ?
在客户端 HTML 标记中读取这些设置的正确方法是什么?
Thanks
谢谢
回答by Scott Mitchell
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:
除了使用<%=ConfigurationManager.AppSettings["MyAttribute"]%>
,正如其他人所指出的,您还可以使用表达式构建器。语法有点不同。而不是<%=...%>
你使用<%$ AppSettings: MyAttribute %>
,像这样:
<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.
如果您只是将 appSettings 值直接转储到静态 HTML 中(我假设您在此示例中),则这两种方法对于所有实际目的都是相同的。
What is nice about expression builders, though, is that you can use them to declaratively assign appSettings values to Web control properties, something you cannotdo with the <%=...%>
syntax. That is, with expression builders you can do something like:
不过,表达式构建器的优点在于您可以使用它们以声明方式将 appSettings 值分配给 Web 控件属性,这是您无法使用<%=...%>
语法实现的。也就是说,使用表达式构建器,您可以执行以下操作:
<asp:Label runat="server" ... Text="<%$ AppSettings: MyAttribute %>" />
Whereas you could notdo:
而你不能这样做:
<asp:Label runat="server" ... Text="<%=ConfigurationManager.AppSettings["MyAttribute"]%>" />
回答by huysorng
The following code:
以下代码:
<%$ AppSettings: MyAttribute %>
is not compatible with general HTML markup and JavaScript function! It's good for asp tag.
与一般 HTML 标记和 JavaScript 功能不兼容!这对asp标签很有用。
Whereas
然而
<%=ConfigurationManager.AppSettings("MyAttribute")%>
really work in general HTML markup.
真正适用于一般的 HTML 标记。
so
所以
<%=ConfigurationManager.AppSettings("MyAttribute")%>
is my recommendation!
是我的推荐!
回答by Dustin Laine
You can use the ConfigurationManager
in you ASPX page. Then you can add in your OBJECT
tag parameters:
您可以ConfigurationManager
在您的 ASPX 页面中使用 。然后你可以添加你的OBJECT
标签参数:
Web.Config
网页配置
</configuration>
<appSettings>
<add key="Setting" value="Value"/>
<appSettings>
</configuration>
ASPX
ASPX
<object>
<param name="Setting" value="<%= System.Configuration.ConfigurationManager.AppSettings["Setting"] %>" />
</object>
回答by Ender
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:
你有几个选择。如果将runat="server"
属性添加到对象标记,则可以使用其 ID 从代码隐藏访问它,并以这种方式添加属性:
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.
无论哪种方式都应该完成工作。
回答by CesarGon
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.
我建议您在运行时从服务器动态生成您的 OBJECT 标记。通过这种方式,您可以注入从 web.config 文件中读取的任何参数。