我现在可能完全偏离了轨道,所以我只会在这里问这个问题,以便有人可以帮助我。
我想要做的是将 web.config 中存储在 applicationSettings 区域中的值插入到我的 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>
该文件后面是实际设置,如下所示:
<applicationSettings>
<MyApp.Properties.Settings>
<setting name="ImagesUrl" serializeAs="String">
<value>http://resources/images/</value>
</setting>
现在我想在标记中引用上述值,如下所示:
<asp:Image ID="Image1" runat="server" ImageUrl="<%$AppSettings:ImagesUrl%>/Image1.jpg
我知道有一个可用的表达式 ,但我没有使用 web.config 的 appsettings 部分,而是使用 configSection。
编辑:
我相信我只能使用 ExpressionBuilder 来完成此操作,因为我必须将字符串与单个图像名称连接起来。我改变了上面的例子来反映这一点。
我喜欢下面的 Bert Smith 代码解决方案来访问配置部分,只需将其放入表达式生成器中。
我陷入了重写 GetCodeExpression 方法的困境,我将在其中调用配置管理器,但我不明白如何构建参数表达式。
public class SettingsExpressionBuilder: ExpressionBuilder
{
public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
{
return ??
}
EDIT
结果如下所示,适用于所有类型的文件,而不仅仅是图像:
<asp:ScriptReference Path='<%$Code:GetAppSetting("ResourcesUrl","JS/jquery/jquery.jqplot.js")%>'
我只是使用 Microsoft 的示例从表达式生成器返回任何类型的代码:
返回新的 CodeSnippetExpression(entry.Expression);
GetAppSetting 是我的自定义 Page 类中的一个方法。