gpt4 book ai didi

asp.net - 从 web.config applicationSettings 获取值到 ASP.NET 标记中

转载 作者:行者123 更新时间:2023-12-02 16:58:10 25 4
gpt4 key购买 nike

我现在可能完全偏离了轨道,所以我只会在这里问这个问题,以便有人可以帮助我。

我想要做的是将 web.config 中存储在 applicationSettings 区域中的值插入到我的 aspx 标记中。具体来说,我想从 config.php 中读取 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

我知道有一个可用的表达式 <%$ AppSettings: ImagesUrl %>,但我没有使用 web.config 的 appsettings 部分,而是使用 configSection。

编辑:我相信我只能使用 ExpressionBuilder 来完成此操作,因为我必须将字符串与单个图像名称连接起来。我更改了上面的示例以反射(reflect)这一点。

我喜欢下面的 Bert Smith 代码解决方案来访问配置部分,只需将其放入表达式生成器中。我无法从调用配置管理器的地方覆盖 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 函数来检索设置

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, object parsedData, 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 文件夹中。将自定义 Express 的配置添加到 web.config 并从 aspx 调用它,如下所示:

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

关于asp.net - 从 web.config applicationSettings 获取值到 ASP.NET 标记中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6073914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com