小编Nie*_*ler的帖子

从web.config applicationSettings获取ASP.NET标记的价值

我现在可能完全偏离轨道,所以我会在这里问这个,所以有人可以帮助我.

我想要做的是将存储在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类中的一种方法.

asp.net markup web-config configsection expressionbuilder

8
推荐指数
1
解决办法
2万
查看次数