Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display AppSettings in an ASP.NET MVC web application?

Tags:

asp.net-mvc

I need to display a value from the web.config appSettings section into a view.

I am using <%= Html.Label %> to populate

In ASP.NET, I'd use ConfigurationSettings.AppSettings["FileServer"].

How do I do this in MVC??

like image 227
Rita Avatar asked Jan 26 '10 23:01

Rita


3 Answers

You should be able to just use

<%=  ConfigurationManager.AppSettings["FileServer"] %>

in your View.

By the way, ConfigurationSettings is deprecated - you should use ConfigurationManager

like image 180
womp Avatar answered Sep 22 '22 09:09

womp


Another pattern, use AppSettingsExpressionBuilder.

<asp:Literal ID="Literal1" runat="server" Text="<%$ AppSettings: sample%>" /> 
like image 40
takepara Avatar answered Sep 22 '22 09:09

takepara


Put the value into TempData["MyVariableName"] using the AppSettings["MyVariableName"] method and then put the TempData value in your view.

In your controller:

TempData["FileServer"] = ConfigurationSettings.AppSettings["FileServer"]

In your view:

like image 25
Nick DeVore Avatar answered Sep 22 '22 09:09

Nick DeVore