8

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.

3
  • "I might be completely off track by now".. I'd say that if you are storing links to image resources in a web.config file, then yes you are very much off track.
    – NotMe
    May 20, 2011 at 15:34
  • Upon reflection, I might be reading this wrong. Are you just storing the beginning part of the external reference or the entire image location? Which leads to the next question: is this to actually support multiple environments (test, stage, prod)?
    – NotMe
    May 20, 2011 at 15:36
  • Yes just the first part of the URL, so I can change that in different environments May 23, 2011 at 10:36

3 Answers 3

12

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>
4
  • I like the idea, but I can't use custom functions in server controls. "Server tags cannot container <%...%> constructs" May 23, 2011 at 10:41
  • Hopefully the follow up section is what your looking for. If need be I can post the entire class, but it looked like you were headed in the right direction.
    – Bert Smith
    May 23, 2011 at 12:51
  • This is not what I implemented in the end but it came close. Problem is, I still can't append the image name, unless maybe I can take the "entry" string apart in the exprBuilder, or have more than one parameter. So I now use another expressionBuilder I already had from a blog post, that compiles any valid C# code. I created a method that returns the requested appSetting string and I can just concatenate the image name. <%$Code:GetAppSetting("ResourcesUrl") + "images/image.jpg" %> May 24, 2011 at 8:54
  • What little doc you can find on deploying a WCF Service library project tells you to copy the App.Config code into Web.config. So you get the <applicationSettings><MyApp.Properties.Settings> section in there. This is the perfect answer for that situation.
    – B H
    Nov 15, 2023 at 18:15
1

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.

0

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:Image ID="Image1" runat="server" 
           ImageUrl="<%$MyApp.Properties.Settings.Default.ImagesUrl%>

It would definitely be easier to store this in <appSettings> though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.