Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

EDIT: 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.

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

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

return new CodeSnippetExpression(entry.Expression);

And GetAppSetting is a method in my custom Page class.

like image 601
Niels Ziegler Avatar asked Feb 02 '23 18:02

Niels Ziegler


2 Answers

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

Then in the aspx markup I call this function

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

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. 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>
like image 144
Bert Smith Avatar answered Feb 05 '23 15:02

Bert Smith


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.

like image 23
gbs Avatar answered Feb 05 '23 17:02

gbs