从web.config applicationSettings到ASP.NET标记中获取价值 [英] Get value from web.config applicationSettings into ASP.NET markup

查看:143
本文介绍了从web.config applicationSettings到ASP.NET标记中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到现在我可能已经完全偏离了轨道,所以我只想在这里问这个问题,以便有人可以帮助我.

I might be completely off track by now, so I will just ask this here so someone can help me.

我想做的是将存储在applicationSettings区域中的web.config中的值插入到我的aspx标记中.具体来说,我想从config中读取URL.这是我使用的configSection设置

What I want to do, is to insert a value from my web.config, stored in an applicationSettings area, into my aspx markup. Specifically I want to reade a URL from config. This is the configSection setup I use

<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>

该文件中的最新设置如下:

Later in that file are the actual settings like so:

<applicationSettings>
<MyApp.Properties.Settings>
  <setting name="ImagesUrl" serializeAs="String">
    <value>http://resources/images/</value>
  </setting>

现在,我想像这样在标记中引用上述值:

Now I want to reference the above value in markup like this:

 <asp:Image ID="Image1" runat="server" ImageUrl="<%$AppSettings:ImagesUrl%>/Image1.jpg

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

I know there's an expression available <%$ AppSettings: ImagesUrl %>, but I'm not using the appsettings part of web.config, rather the configSection.

我相信我只能用ExpressionBuilder做到这一点,因为我必须将字符串与单个图像名称连接起来.我更改了上面的示例以反映这一点.

I believe I can only do it with ExpressionBuilder, because I have to concatenate the string with the individual image name. I changed the example above to reflect that.

我喜欢下面的Bert Smith代码解决方案来访问config部分,只需要将其放入表达式生成器中即可. 我被困在重写GetCodeExpression方法,从那里我将调用配置管理器,但是我不了解如何构建参数表达式.

I like Bert Smith Code Solution below for accessing the config section, only I need to put it in an expression builder. I'm stuck at overriding the GetCodeExpression method from where I would call the Configuration Manager, but I don't understand how to build an expression the parameters.

public class SettingsExpressionBuilder: ExpressionBuilder
{
    public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
    {
        return ??
    }

编辑
结果看起来像这样,并且适用于所有类型的文件,而不仅仅是图像:

EDIT
The result looks like this, and works for all kinds of files, not just images:

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

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

and I simply used the example from Microsoft to return any kind of code from the expression builder:

返回新的CodeSnippetExpression(entry.Expression);

return new CodeSnippetExpression(entry.Expression);

GetAppSetting是我的自定义Page类中的方法.

And GetAppSetting is a method in my custom Page class.

推荐答案

通常,您将创建一个自定义设置类以按以下方式读取这些值:

Typically you would create a custom settings class to read these values out as this artical describes. Personally, I would just use the appSettings as suggested above as this is existing functionality and for what your doing would on the surface seem adequate.

但是,由于不知道您的情况,如果没有这样的自定义设置,您的尝试可以解决:

However, not knowing your circumstances, what your attempting to do could be solved without the custom settings like so:

在后面的代码中,我创建了一个受保护的函数来检索设置

In the code behind I created a protected function to retrieve the setting

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标记中,我将此函数称为

Then in the aspx markup I call this function

<div>
    <label runat="server" id="label"><%=GetCustomSetting("applicationSettings/MyApp.Properties.Settings", "ImagesUrl") %></label>
</div>

希望这会有所帮助.

跟进:

CodeExpression看起来像这样:

The CodeExpression will look something like this:

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

In my Test, I created a class called CustomSettingsExpressionBuilder and added it to the App_Code folder. Added the configuration for the custom express to the web.config and called it from aspx like so:

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

这篇关于从web.config applicationSettings到ASP.NET标记中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆