[英]Trying to get key and value from appSettings
我正在尝试从我的项目中的appSettings中获取信息,因此我正在使用它,因此,如果我需要编辑信息,我可以只更改应用程序设置页面,我想将键和值提取为字符串并显示到页面。 这是我的代码。 在我拥有的appSettings中,
<?xml version="1.0"?>
<appSettings>
<add key ="General" value="someValue1"/>
<add key ="Other" value="otherValue"/>
</appSettings>
在控制器页面中,我有
namespace MyPro.Web.Controllers
{
using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using Models.MyPro;
public class MyController
{
public ActionResult MyPro()
{
Dictionary<string, dynamic> myProData = new Dictionary<string, dynamic>();
var general = ConfigurationManager.AppSettings["General"];
var other = ConfigurationManager.AppSettings["Other"];
myProData.Add("general", general);
myPrData.Add("other", other);
var myProModel = new myProModel(myProData);
return View(myProModel);
}
}
}
在我的模型中
namespace MyPro.Web.Models.MyPro
{
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MyPro.Model;
public class MyProModel
{
public MyProModel(Dictionary<string, dynamic> myProData)
{
this.MyProData = myProData;
}
public Dictionary<string, dynamic> MyProData { get; private set; }
}
}
在cshtml页面中
@model MyPro.Web.Models.MyPro.MyProModel
<div class="content">
<h1 class="sectionHeading">Contact Us</h1>
@foreach (var item in Model.MyProData)
{
<div class="contentSection">
<h2>Tables</h2>
<div class="innerContentSection">
<table class="baseTable">
<thead><tr><th>Column1</th><th>Colum2</th></tr></thead>
<tbody>
<tr><td>@Model.MyProData</td><td>@Model.MyProData["general"].</td></tr>
<tr><td>@Model.MyProData</td><td>@Model.MyProData["other"]</td></tr>
</tbody>
</table>
</div>
</div>
}
</div>
我的问题是如何从appSettings的值中获取column1来显示字符串general和other,以及column2来显示来自appSettings的值的someValue1和otherValue,这是执行我要完成的工作的好习惯还是有更好的方法 我已经尝试过使用列表,哈希表等从堆栈中使用其他方法。是否有一种简单的方法可以只从键和值中获取或提取数据。
我认为这样会起作用
public class MyProModel
{
public Dictionary<string, string> MyProData { get; set; }
}
var appSettings = ConfigurationManager.AppSettings;
var myProModel = new MyProModel
{
MyProData = appSettings.AllKeys.ToDictionary(k => k, k => appSettings[k])
};
您可能想要使用AppSettings的AllKeys属性。 例如:
public ActionResult MyPro()
{
Dictionary<string, dynamic> myProData = new Dictionary<string, dynamic>();
ConfigurationManager.AppSettings.AllKeys.ToList().ForEach
(
settingsKey =>
myProData.Add(settingsKey, ConfigurationManager.AppSettings[settingsKey]);
);
var myProModel = new myProModel(myProData);
return View(myProModel);
}
如下所述,如果要输出键的子集...
首先将一个新项目添加到您的配置中:
<add key ="ConfigItemsToMonitor" value="General,Other"/>
然后使用此键生成要输出的列表:
public ActionResult MyPro()
{
Dictionary<string, dynamic> myProData = new Dictionary<string, dynamic>();
ConfigurationManager.AppSettings["ConfigItemsToMonitor"].Split(',').ToList().ForEach
(
settingsKey =>
myProData.Add(settingsKey, ConfigurationManager.AppSettings[settingsKey]);
);
var myProModel = new myProModel(myProData);
return View(myProModel);
}
您可以决定还监视ConfigItemsToMonitor的值-只需将其添加到逗号分隔的值列表中即可。 确保排除空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.