Nie*_*ler 8 asp.net markup web-config configsection expressionbuilder
我现在可能完全偏离轨道,所以我会在这里问这个,所以有人可以帮助我.
我想要做的是将存储在applicationSettings区域的web.config中的值插入到我的aspx标记中.具体来说,我想从配置中恢复URL.这是我使用的configSection设置
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=123456">
<section name="MyApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=12345" requirePermission="false" />
</configSections>
Run Code Online (Sandbox Code Playgroud)
稍后在该文件中的实际设置如下:
<applicationSettings>
<MyApp.Properties.Settings>
<setting name="ImagesUrl" serializeAs="String">
<value>http://resources/images/</value>
</setting>
Run Code Online (Sandbox Code Playgroud)
现在我想在标记中引用上面的值,如下所示:
<asp:Image ID="Image1" runat="server" ImageUrl="<%$AppSettings:ImagesUrl%>/Image1.jpg
Run Code Online (Sandbox Code Playgroud)
我知道有一个表达式<%$ AppSettings:ImagesUrl%>,但我没有使用web.config的appsettings部分,而是使用configSection.
编辑:我相信我只能用ExpressionBuilder来做,因为我必须将字符串与单个图像名称连接起来.我改变了上面的例子来反映这一点.
我喜欢下面的Bert Smith Code Solution来访问配置部分,只需要将它放在表达式构建器中.我坚持要从我调用配置管理器的地方覆盖GetCodeExpression方法,但我不明白如何构建表达式参数.
public class SettingsExpressionBuilder: ExpressionBuilder
{
public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
{
return ??
}
Run Code Online (Sandbox Code Playgroud)
编辑
结果如下所示,适用于各种文件,而不仅仅是图像:
<asp:ScriptReference Path='<%$Code:GetAppSetting("ResourcesUrl","JS/jquery/jquery.jqplot.js")%>'
Run Code Online (Sandbox Code Playgroud)
我只是使用Microsoft的示例从表达式构建器返回任何类型的代码:
返回新的CodeSnippetExpression(entry.Expression);
GetAppSetting是我的自定义Page类中的一种方法.
Ber*_*ith 11
通常,您将创建一个自定义设置类,因为这读取这些值超出ARTICAL描述.就个人而言,我只会使用上面建议的appSettings,因为这是现有的功能,而你表面上的表现似乎足够了.
但是,不了解您的情况,您尝试做的事情可以在没有自定义设置的情况下解决:
在后面的代码中,我创建了一个受保护的函数来检索设置
protected string GetCustomSetting(string Section, string Setting)
{
var config = ConfigurationManager.GetSection(Section);
if (config != null)
return ((ClientSettingsSection)config).Settings.Get(Setting).Value.ValueXml.InnerText;
return string.Empty;
}
Run Code Online (Sandbox Code Playgroud)
然后在aspx标记中我调用此函数
<div>
<label runat="server" id="label"><%=GetCustomSetting("applicationSettings/MyApp.Properties.Settings", "ImagesUrl") %></label>
</div>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
跟进:
CodeExpression看起来像这样:
public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
{
var config = ConfigurationManager.GetSection("applicationSettings/MyApp.Properties.Settings");
return new CodePrimitiveExpression(((ClientSettingsSection)config).Settings.Get(entry.Expression).Value.ValueXml.InnerText);
}
Run Code Online (Sandbox Code Playgroud)
在我的测试中,我创建了一个名为的类CustomSettingsExpressionBuilder,并将其添加到App_Code文件夹中.将自定义快速的配置添加到web.config并从aspx中调用它,如下所示:
<asp:Label ID="Label1" runat="server" Text="<%$CustomSettings:ImagesUrl %>"></asp:Label>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21139 次 |
| 最近记录: |