Binding ASP.Net Web.Config Settings To .ASPX File ?

The Scenario

I have an ASP.NET web project. I want to be able to define all of the links for the site inside my web.config file so that they can be changed easily if needs be. Currently I have an “” section in my web.config file.

The Question

How do I bind this key value pair to an ” tag in my .aspx file?!

The App Settings in My Web.Config File

<appSettings>
    <add key="MyNewLink" value="http://someurl.co.uk/" />
</appSettings>

Help greatly appreciated.

EDIT:

Sorry I should have mentioned that this is for a html link: **<a href></a>**

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In your aspx file it would be:

NavigateUrl='<%$ AppSettings:MyNewLink %>'

and the full <a> tag is defined as:
<a runat="server" href="<%$ AppSettings:MyNewLink %>" rel="nofollow noreferrer noopener">Text link</a>

This syntax can only be used on an ASP.NET WebForms server control.

Method 2

Isn’t this what a .sitemap file is for?

Anyway, as far as I know, you will have to ‘bind’ this from code behind. Something like:

hlYourLink.NavigateUrl = ConfigurationManager.AppSettings["MyNewLink"];

Method 3

I ended up using this……

.aspx file

<asp:literal id="litgetquote" runat="server"></asp:literal>

.aspx.cs CODE BEHIND

litgetquote.Text = "<A HREF='" + ConfigurationManager.AppSettings["GetQuoteUrl"] + "'>" +
            "get a quote now" + "</A>";


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x