tags:

views:

252

answers:

4

Hi

I need to display a value from Web.Config Appsettings section onto View. I have <%= Html.Label on this View where i need to populate from Appsettings.

Ex: If it in ASP.NET, we use ConfigurationSettings.AppSettings["FileServer"].

How can we achieve this in MVC??

Appreciate your responses.

+1  A: 

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:

<%= TempData["FileServer"] %>

Nick DeVore
+2  A: 

You should be able to just use

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

in your View.

ConfigurationSettings is deprecated by the way - you should use ConfigurationManager.

womp
A slight segue but seeing as this is a correct answer, I'd just like to add that any use of magic strings in the app (and certainly the views) is a bit smelly and I would consider using a facade wrapper class on the app settings in order to avoid this. Calls like `<%= App.FileServer %>` are so much nicer!
Dan Atkinson
A: 

You can do this the same way but this is bad practice. You should prepare all data for display in controller and pass it to the view.

Passing data to the view by ViewData Collection or you can create typed view.

More on that: http://msdn.microsoft.com/en-us/library/dd394711.aspx

dario-g
+1  A: 

Another pattern, use AppSettingsExpressionBuilder.

<asp:Literal ID="Literal1" runat="server" Text="<%$ AppSettings: sample%>" /> 
takepara