更新获取配置文件

This commit is contained in:
2018-05-10 23:25:21 +08:00
parent 00e8273b75
commit 9642d44c5c
5 changed files with 90 additions and 15 deletions
+53 -11
View File
@@ -10,11 +10,20 @@ using COMCMS.Core;
using XCode;
using System.Text;
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
namespace COMCMS.Web.Controllers
{
public class HomeController : Controller
{
public static IConfiguration Configuration { get; set; }
public HomeController()
{
Configuration = new ConfigurationBuilder()
.Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
.Build();
}
public IActionResult Index()
{
return View();
@@ -32,12 +41,12 @@ namespace COMCMS.Web.Controllers
public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";
Config cfg = Config.FindById(1);
ViewBag.siteName = cfg.SiteName;
string prekey = Utils.PrefixKey;
Article a = Article.FindById(1);
ViewBag.siteName = a.Title;
ViewBag.c = CookiesHelper.GetCookie("abc");
ViewBag.d = Utils.GetServerUrl();
ViewBag.d = prekey;
return View();
}
@@ -49,15 +58,48 @@ namespace COMCMS.Web.Controllers
#region 测试
public IActionResult Test()
{
JsonTip json = new JsonTip();
string strJson = JsonTip.GetJsonString(json);
var css = Configuration.GetSection("connectionStrings");
var list = css.GetChildren();
if(list !=null && list.Count() > 0)
{
//Byte[] bytes = Encoding.UTF8.GetBytes(strJson);
//Stream stream = new MemoryStream(bytes);
//MyHttpContext.Current.Response.ContentType = "text/plain";
//MyHttpContext.Current.Response.Body = stream;
}
string s = "";
foreach (var item in list)
{
string connName = item.Path.Substring(item.Path.IndexOf(":") + 1);
string connectionString = css.GetSection(connName).GetSection("connectionString").Value;
string providerName = css.GetSection(connName).GetSection("providerName").Value;
s += item.Path.Substring(item.Path.IndexOf(":")+1)+ "|"+ Configuration.GetSection(item.Path+ ":connectionString").Value+"|" + Configuration.GetSection(item.Path + ":providerName").Value +"|"+connectionString+"|"+ providerName +"|||" + list.Count();
}
return Content("ok");
return Content(s);
}
public IActionResult Test2()
{
var settings = "appsettings.json".GetFullPath();
var css2 = new ConfigurationBuilder().AddJsonFile(settings).Build().GetSection("connectionStrings");
string s = "";
if (css2 != null)
{
foreach (var item in css2.GetChildren())
{
var name = item.Path.Substring(item.Path.IndexOf(":") + 1);// item["name"];
var constr = item["connectionString"];
var provider = item["providerName"];
s += $"{name}|{constr}|{provider}|||";
//var type = DbFactory.GetProviderType(constr, provider);
//if (type == null) XTrace.WriteLine("无法识别{0}的提供者{1}!", name, provider);
//cs.Add(name, constr);
//_connTypes.Add(name, type);
}
}
return Content(s);
}
#endregion
}