简体   繁体   English

如何在MVC中显示app.config文件

[英]How to display app.config file in mvc

I have seen some old ways to accomplish this like this one . 我已经看到了一些旧的方式来做到这一点像这一个

Yet even this display returns: 然而,即使此显示返回:

<%=ConfigurationManager.AppSettings["webpages:Version"].ToString() %>

I assume I could be using something that is outdated in .Net universe. 我认为我可能会使用.Net Universe中过时的东西。 The goal is to loop through the values and package it back in a log.html page. 目标是遍历这些值,并将其打包回log.html页中。

Well first of all, 90% of "tutorials" on CodeProject are utter crap, just as the one you link to. 首先,就像您链接到的一样,CodeProject上的“教程”中有90%都是垃圾。 The title of it ( "Read Configuration Settings of Web.config using Javascript" ) is a lie in and of itself, as it is definitely impossible to read the web.config from JavaScript. 它的标题( “使用Javascript读取Web.config的配置设置” )本身就是一个谎言,因为从JavaScript读取web.config绝对是不可能的。

Second, you seem to be reading ASP.NET WebForms tutorials. 其次,您似乎正在阅读ASP.NET WebForms教程。 Start by looking for an MVC tutorial, preferably on http://www.asp.net . 首先寻找MVC教程,最好在http://www.asp.net上

This is quite trivial using MVC. 使用MVC相当简单。 You create a model to hold the values, an action method that will process the request and a view to display the model's values. 您将创建一个用于保存值的模型,一个将处理请求的操作方法以及一个用于显示模型值的视图。

public class ConfigurationValuesViewModel
{
    public List<KeyValuePair<string, string>> AppSettingsValues { get; private set; }

    public ConfigurationValuesViewModel()
    {
        AppSettingsValues = new List<KeyValuePair<string, string>>();
    }
}

Controller: 控制器:

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);
}

And the view: 和视图:

@model ConfigurationValuesViewModel

@foreach (var setting in Model.AppSettingsValues)
{
    @setting.Key - @setting.Value
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM