46

I've created a web page and it contains some settings value in Web.Config for example images. So I want to give the path of images in Web.Config file and file name in that particular image src.

I wanted to read that settings only in aspx page not in codebehind.

For example

Below is my code:

Web.Config:
<add key="ImagePath" value="http://192.168.0.181/Labeling/Images/"/>

and in my aspx page,

<img id="ImgHeader" runat="server" src="<%ConfigurationManager.AppSettings["ImagePath"]%>" />
Disman38
  • 7
  • 4
Sandy
  • 6,285
  • 15
  • 65
  • 93
  • 1
    below answers are correct, but you should also think about creating a user control (with just the image tag/inline code), to avoid magic strings in all the affected ASPX/ASCX's. – RPM1984 Nov 11 '10 at 09:22
  • 2
    <%$ AppSettings:ImagePath %> http://stackoverflow.com/questions/1559446/binding-asp-net-web-config-settings-to-aspx-file-a-href-a – Muhammad Waqas Iqbal Sep 29 '12 at 14:11

4 Answers4

45
<img id="ImgHeader" runat="server" src="<%$ ConfigurationSettings.AppSettings["ImagePath"] %>" />

Should do the trick.

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
  • 3
    Error: Server tags cannot contain <% ... %> constructs. – Matthew Lock Aug 29 '13 at 01:55
  • 12
    You should use ConfigurationManager instead of ConfigurationSettings (now obsolete) unless you are using .Net 2 or less. – Dave Nov 07 '13 at 19:56
  • 2
    Use single quotes if you're still getting the Server tags error. src='<%= ConfigurationManager.AppSettings["ImagePath"] %>' /> – JDNickell Oct 28 '14 at 18:42
16

Waqas Iqbal's answer:

<%$ AppSettings:ImagePath %> 

Eg:

<asp:Literal runat="server" Text="<%$ AppSettings:ImagePath %>

from Binding ASP.Net Web.Config Settings To .ASPX File <a href></a>? worked nicely!

Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
7
<%= ConfigurationSettings.AppSettings["ImagePath"] %>
x2.
  • 9,554
  • 6
  • 41
  • 62
  • 5
    Parser Error Message: Server tags cannot contain <% ... %> constructs. Source Error: Line 234: Line 235: Line 236: ')" /> Line 237: Line 238: – Sandy Nov 11 '10 at 09:35
  • Note: You can get this error if your page is set for VisualBasic instead of C#. In that case, change the square brackets to round brackets (parentheses). Example: '<%= ConfigurationManager.AppSettings("HtmlOutputPath") %>' – Mark A. Durham Jun 07 '18 at 17:48
2

This worked for me:

<%= ConfigurationManager.AppSettings("ImagePath") %>
jbyrd
  • 5,287
  • 7
  • 52
  • 86
  • 3
    ConfigurationManager and brackets is what worked for me.... <%= ConfigurationManager.AppSettings["ImagePath"] %> – smoore4 Sep 17 '18 at 16:49