简体   繁体   English

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

[英]Get value from web.config applicationSettings into ASP.NET markup

I might be completely off track by now, so I will just ask this here so someone can help me. 我现在可能完全偏离轨道,所以我会在这里问这个,所以有人可以帮助我。

What I want to do, is to insert a value from my web.config, stored in an applicationSettings area, into my aspx markup. 我想要做的是将存储在applicationSettings区域的web.config中的值插入到我的aspx标记中。 Specifically I want to reade a URL from config. 具体来说,我想从配置中恢复URL。 This is the configSection setup I use 这是我使用的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>

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

I know there's an expression available <%$ AppSettings: ImagesUrl %>, but I'm not using the appsettings part of web.config, rather the configSection. 我知道有一个表达式<%$ AppSettings:ImagesUrl%>,但我没有使用web.config的appsettings部分,而是使用configSection。

EDIT: I believe I can only do it with ExpressionBuilder, because I have to concatenate the string with the individual image name. 编辑:我相信我只能用ExpressionBuilder来做,因为我必须将字符串与单个图像名称连接起来。 I changed the example above to reflect that. 我改变了上面的例子来反映这一点。

I like Bert Smith Code Solution below for accessing the config section, only I need to put it in an expression builder. 我喜欢下面的Bert Smith Code Solution来访问配置部分,只需要将它放在表达式构建器中。 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. 我坚持要从我调用配置管理器的地方覆盖GetCodeExpression方法,但我不明白如何构建表达式参数。

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")%>'

and I simply used the example from Microsoft to return any kind of code from the expression builder: 我只是使用Microsoft的示例从表达式构建器返回任何类型的代码:

return new CodeSnippetExpression(entry.Expression); 返回新的CodeSnippetExpression(entry.Expression);

And GetAppSetting is a method in my custom Page class. GetAppSetting是我的自定义Page类中的一种方法。

Typically you would create a custom settings class to read these values out as this artical describes. 通常,您将创建一个自定义设置类,因为这读取这些值超出ARTICAL描述。 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. 就个人而言,我只会使用上面建议的appSettings,因为这是现有的功能,而你表面上的表现似乎足够了。

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

Then in the aspx markup I call this function 然后在aspx标记中我调用此函数

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

Hope this helps. 希望这可以帮助。

Follow Up: 跟进:

The CodeExpression will look something like this: 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);
}

In my Test, I created a class called CustomSettingsExpressionBuilder and added it to the App_Code folder. 在我的测试中,我创建了一个名为CustomSettingsExpressionBuilder的类,并将其添加到App_Code文件夹中。 Added the configuration for the custom express to the web.config and called it from aspx like so: 将自定义快速的配置添加到web.config并从aspx中调用它,如下所示:

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

Does it has to be in markup? 它必须是标记吗? Why don't you set it in code-behind. 为什么不在代码隐藏中设置它。

Image1.ImageUrl= //fetch your settings here.

One another way would be defining a property or static method in your code-behind and then using that in the markup. 另一种方法是在代码隐藏中定义属性或静态方法,然后在标记中使用它。

I'm not sure about the ASP.NET bit of it, but if this was normal code you'd do MyApp.Properties.Settings.Default.ImagesUrl , so try 我不确定它的ASP.NET位,但如果这是普通的代码你会做MyApp.Properties.Settings.Default.ImagesUrl ,所以试试

<asp:Image ID="Image1" runat="server" 
           ImageUrl="<%$MyApp.Properties.Settings.Default.ImagesUrl%>

It would definitely be easier to store this in <appSettings> though. 尽管如此,将它存储在<appSettings>肯定会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM