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

家住魔仙堡 提交于 2019-12-29 07:06:36

问题


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


回答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 %>">Text link</a>

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




回答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"];



回答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>";


来源:https://stackoverflow.com/questions/1559446/binding-asp-net-web-config-settings-to-aspx-file-a-href-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!