How to access web.config settings directly in .aspx page?
Asked Answered
T

4

47

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"]%>" />
Tuque answered 11/11, 2010 at 9:15 Comment(2)
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.Mouser
<%$ AppSettings:ImagePath %> #1559946Lancashire
S
45
<img id="ImgHeader" runat="server" src="<%$ ConfigurationSettings.AppSettings["ImagePath"] %>" />

Should do the trick.

Scupper answered 11/11, 2010 at 9:17 Comment(3)
Error: Server tags cannot contain <% ... %> constructs.Naker
You should use ConfigurationManager instead of ConfigurationSettings (now obsolete) unless you are using .Net 2 or less.Deviation
Use single quotes if you're still getting the Server tags error. src='<%= ConfigurationManager.AppSettings["ImagePath"] %>' />Rill
N
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!

Naker answered 29/8, 2013 at 2:1 Comment(0)
O
7
<%= ConfigurationSettings.AppSettings["ImagePath"] %>
Othilia answered 11/11, 2010 at 9:18 Comment(2)
Parser Error Message: Server tags cannot contain <% ... %> constructs. Source Error: Line 234: <asp:Button ID="btnSubmit" Text="Submit" runat="server" /></td> Line 235: <td> Line 236: <asp:Button ID="btnDisplay" Text="Display Page" runat="server" OnClientClick="window.open('<%= ConfigurationManager.AppSettings["HtmlOutputPath"] %>')" /></td> Line 237: </tr> Line 238: </table>Tuque
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") %>'Felicio
P
2

This worked for me:

<%= ConfigurationManager.AppSettings("ImagePath") %>
Pyles answered 5/10, 2016 at 17:30 Comment(1)
ConfigurationManager and brackets is what worked for me.... <%= ConfigurationManager.AppSettings["ImagePath"] %>Overblown

© 2022 - 2024 — McMap. All rights reserved.