diff --git a/COMCMS.Common/Utils.cs b/COMCMS.Common/Utils.cs index 390a613..97aecdf 100644 --- a/COMCMS.Common/Utils.cs +++ b/COMCMS.Common/Utils.cs @@ -7,6 +7,8 @@ using System.Reflection; using System.Configuration; using System.Net; using System.IO; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Configuration.Json; namespace COMCMS.Common { @@ -15,6 +17,17 @@ namespace COMCMS.Common /// public class Utils { + + public static IConfiguration Configuration { get; set; } + + static Utils() + { + //ReloadOnChange = true 当appsettings.json被修改时重新加载 + Configuration = new ConfigurationBuilder() + .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true }) + .Build(); + } + #region 静态变量 /// /// 得到正则编译参数设置 @@ -30,7 +43,7 @@ namespace COMCMS.Common { get { - string strPrefixKey = ConfigurationManager.AppSettings["COMCMSPrefixKey"]; + string strPrefixKey = Configuration["SystemSetting:COMCMSPrefixKey"];// ConfigurationManager.AppSettings["COMCMSPrefixKey"]; if (string.IsNullOrEmpty(strPrefixKey)) { strPrefixKey = "comcmsntc_"; diff --git a/COMCMS.Core/COMCMS.Core.csproj b/COMCMS.Core/COMCMS.Core.csproj index cda98f1..64a8f85 100644 --- a/COMCMS.Core/COMCMS.Core.csproj +++ b/COMCMS.Core/COMCMS.Core.csproj @@ -5,7 +5,7 @@ - + diff --git a/COMCMS.Web/COMCMS.Web.csproj b/COMCMS.Web/COMCMS.Web.csproj index 61e9331..591300b 100644 --- a/COMCMS.Web/COMCMS.Web.csproj +++ b/COMCMS.Web/COMCMS.Web.csproj @@ -9,7 +9,7 @@ - + @@ -273,4 +273,13 @@ + + + Always + + + Always + + + diff --git a/COMCMS.Web/Controllers/HomeController.cs b/COMCMS.Web/Controllers/HomeController.cs index b051394..a8b9c85 100644 --- a/COMCMS.Web/Controllers/HomeController.cs +++ b/COMCMS.Web/Controllers/HomeController.cs @@ -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 } diff --git a/COMCMS.Web/appsettings.json b/COMCMS.Web/appsettings.json index 85ca42e..4088ad9 100644 --- a/COMCMS.Web/appsettings.json +++ b/COMCMS.Web/appsettings.json @@ -8,6 +8,17 @@ "SystemSetting": { "FileAllowedExtensions": "zip,rar,7z,doc,docx,xls,xlsx,ppt,pptx,csv,pdf,txt,gz,gzip,gif,png,jpg,jpeg,bmp,avi,fla,flv,swf,mp4,mp3,mov,mid,mpg,mpeg,rm,rmvb,wma,wav,wmv", "ImageAllowedExtensions": "gif,png,jpg,jpeg,bmp", - "MediaAllowedExtensions": "flv,mp4,mp3,wmv,swf,mov" + "MediaAllowedExtensions": "flv,mp4,mp3,wmv,swf,mov", + "COMCMSPrefixKey": "abc_" + }, + "connectionStrings": { + "dbconn": { + "connectionString": "Server=.;Port=3306;Database=comcmscore;Uid=root;Pwd=root;", + "providerName": "MySql.Data.MySqlClient" + }, + "mssqlconnon": { + "connectionString": "server=(local);database=comcmscore;uid=sa;pwd=123456", + "providerName": "System.Data.SqlClient" + } } }