我需要顯示一個值從web.config
appSettings
部分到一個視圖。如何在ASP.NET MVC Web應用程序中顯示AppSettings?
我使用<%= Html.Label %>
填充
在ASP.NET中,我會使用ConfigurationSettings.AppSettings["FileServer"]
。
如何在MVC中執行此操作?
我需要顯示一個值從web.config
appSettings
部分到一個視圖。如何在ASP.NET MVC Web應用程序中顯示AppSettings?
我使用<%= Html.Label %>
填充
在ASP.NET中,我會使用ConfigurationSettings.AppSettings["FileServer"]
。
如何在MVC中執行此操作?
你應該能夠使用
<%= ConfigurationManager.AppSettings["FileServer"] %>
在您的視圖。
順便說一句,ConfigurationSettings
已經過時 - 你應該使用ConfigurationManager
把值到TempData的使用AppSettings [「MyVariableName」]方法[「MyVariableName」],然後把TempData的值在您的視圖。
在你的控制器:
的TempData [ 「文件服務器」] = ConfigurationSettings.AppSettings [ 「文件服務器」]
在你看來:
爲此,您可以以同樣的方式,但是這是不好的做法。您應準備所有數據以在控制器中顯示並將其傳遞到視圖。
通過ViewData Collection將數據傳遞給視圖,或者您可以創建分類視圖。
你可以檢查更多關於here。
另一種模式是使用AppSettingsExpressionBuilder。
<asp:Literal ID="Literal1" runat="server" Text="<%$ AppSettings: sample%>" />
輕微賽格瑞但看到這是一個正確的答案,我只是想補充一點,在應用程序中使用任何魔法的字符串(當然的意見)是一個有點臭,我會考慮使用爲了避免這種情況,應用程序設置上的外觀包裝類。像'<%= App.FileServer%>這樣的調用非常好! – 2010-01-27 02:07:46
確保導入System.Configuration在 頁面頂部@using System.Configuration(MVC)或<%@導入命名空間=「System.Configuration」%>爲web表單 – Keith 2011-09-27 21:34:26