C# 如何在mvc中显示app.config文件
我见过一些像这样的老办法 然而,即使是这样的显示也会返回:C# 如何在mvc中显示app.config文件,c#,model-view-controller,web-config,app-config,C#,Model View Controller,Web Config,App Config,我见过一些像这样的老办法 然而,即使是这样的显示也会返回: <%=ConfigurationManager.AppSettings["webpages:Version"].ToString() %> 我想我可能正在使用.Net宇宙中过时的东西。我们的目标是循环遍历这些值并将其打包回log.html页面。首先,CodeProject上90%的“教程”都是一派胡言,就像您链接到的一样。它的标题(“使用Javascript读取Web.config的配置设置”)本身就是一个谎言,因为从
<%=ConfigurationManager.AppSettings["webpages:Version"].ToString() %>
我想我可能正在使用.Net宇宙中过时的东西。我们的目标是循环遍历这些值并将其打包回log.html页面。首先,CodeProject上90%的“教程”都是一派胡言,就像您链接到的一样。它的标题(“使用Javascript读取Web.config的配置设置”)本身就是一个谎言,因为从Javascript读取Web.config绝对是不可能的
其次,您似乎正在阅读ASP.NET WebForms教程。从寻找MVC教程开始,最好是在
使用MVC这是非常简单的。您可以创建一个保存值的模型、一个处理请求的操作方法和一个显示模型值的视图
public class ConfigurationValuesViewModel
{
public List<KeyValuePair<string, string>> AppSettingsValues { get; private set; }
public ConfigurationValuesViewModel()
{
AppSettingsValues = new List<KeyValuePair<string, string>>();
}
}
本教程介绍了调用函数失败的情况。我在谷歌上发现了控制台应用程序,但作为MVC应用程序解决方案,这并不是什么新鲜事。在这个例子中,这个教程太旧了,它使用了Yes,大约90%都是codeProject上的垃圾,但我想在2005年那个网站很棒。谢谢,这开始变得更有意义了。
public ActionResult GetConfigurationValues()
{
// Fill the ViewModel with all AppSettings Key-Value pairs
var model = new ConfigurationValuesViewModel();
foreach (string key in ConfigurationManager.AppSettings.AllKeys)
{
string value = ConfigurationManager.AppSettings[key];
model.AppSettingsValues.Add(new KeyValuePair<string, string>(key, value);
}
return View(model);
}
@model ConfigurationValuesViewModel
@foreach (var setting in Model.AppSettingsValues)
{
@setting.Key - @setting.Value
}