开发者

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

开发者 https://www.devze.com 2022-12-18 17:19 出处:网络
I need to display a value from the web.config ap开发者_Go百科pSettings section into a view. Iam using <%= Html.Label %> to populate

I need to display a value from the web.config ap开发者_Go百科pSettings 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??


You should be able to just use

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

in your View.

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


Another pattern, use AppSettingsExpressionBuilder.

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


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:


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.

You can check more on that here.

0

精彩评论

暂无评论...
验证码 换一张
取 消