HTML   发布时间:2022-04-14  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了从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,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

我知道有一个表达式<%$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 ??
    }

编辑
结果如下所示,适用于各种文件,而不仅仅是图像:

<asp:ScriptReference Path='<%$Code:GetAppSetTing("resourcesUrl","JS/jquery/jquery.jqplot.js")%>'

我只是使用Microsoft的示例从表达式构建器返回任何类型的代码:

返回新的CodeSnippetExpression(entry.Expression);

GetAppSetTing是我自定义Page类中的一种方法.

解决方法

通常,您将创建自定义设置类以读取这些值,如此 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;
}

然后在aspx标记中我调用此函数

<div>
    <label runat="server" id="label"><%=GetCustomSetTing("applicationSetTings/MyApp.properties.SetTings","ImagesUrl") %></label>
</div>

希望这可以帮助.

跟进:

CodeExpression看起来像这样

public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,ExpressionBuilderContext context)
{
    var config = ConfigurationManager.GetSection("applicationSetTings/MyApp.properties.SetTings");
    return new CodePrimitiveExpression(((ClientSetTingsSection)config).SetTings.Get(entry.Expression).Value.ValueXml.InnerText);
}

在我的测试中,我创建了一个名为CustomSetTingsExpressionBuilder的类,并将其添加到App_Code文件夹中.将自定义快速的配置添加到web.config并从aspx中调用它,如下所示:

<asp:Label ID="Label1" runat="server" Text="<%$CustomSetTings:ImagesUrl %>"></asp:Label>

大佬总结

以上是大佬教程为你收集整理的从web.config applicationSettings获取ASP.NET标记的价值全部内容,希望文章能够帮你解决从web.config applicationSettings获取ASP.NET标记的价值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。