如何在ASP.NET MVC Web应用程序中显示AppSettings?

Rit*_*ita 10 asp.net-mvc

我需要将该web.config appSettings部分的值显示在视图中.

我正在使用<%= Html.Label %>填充

在ASP.NET中,我会使用ConfigurationSettings.AppSettings["FileServer"].

我怎么在MVC中这样做?

wom*_*omp 23

你应该可以使用

<%=  ConfigurationManager.AppSettings["FileServer"] %>
Run Code Online (Sandbox Code Playgroud)

在你的视图中.

顺便说一句,ConfigurationSettings已弃用 - 你应该使用ConfigurationManager

  • 确保导入页面顶部的System.Configuration @using System.Configuration(MVC)或<%@ Import namespace ="System.Configuration"%> for webforms (5认同)
  • 虽然这是一个正确的答案,但我只想补充一点,在应用程序中使用魔术字符串(当然还有视图)有点臭,我会考虑在应用程序上使用外观封装类设置以避免这种情况.像`<%= App.FileServer%>`之类的调用非常好! (3认同)

小智 5

另一种模式,使用AppSettingsExpressionBuilder.

<asp:Literal ID="Literal1" runat="server" Text="<%$ AppSettings: sample%>" /> 
Run Code Online (Sandbox Code Playgroud)