更新获取配置文件

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
+14 -1
View File
@@ -7,6 +7,8 @@ using System.Reflection;
using System.Configuration; using System.Configuration;
using System.Net; using System.Net;
using System.IO; using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
namespace COMCMS.Common namespace COMCMS.Common
{ {
@@ -15,6 +17,17 @@ namespace COMCMS.Common
/// </summary> /// </summary>
public class Utils 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 静态变量 #region 静态变量
/// <summary> /// <summary>
/// 得到正则编译参数设置 /// 得到正则编译参数设置
@@ -30,7 +43,7 @@ namespace COMCMS.Common
{ {
get get
{ {
string strPrefixKey = ConfigurationManager.AppSettings["COMCMSPrefixKey"]; string strPrefixKey = Configuration["SystemSetting:COMCMSPrefixKey"];// ConfigurationManager.AppSettings["COMCMSPrefixKey"];
if (string.IsNullOrEmpty(strPrefixKey)) if (string.IsNullOrEmpty(strPrefixKey))
{ {
strPrefixKey = "comcmsntc_"; strPrefixKey = "comcmsntc_";
+1 -1
View File
@@ -5,7 +5,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="NewLife.XCode" Version="9.7.6701.38516" /> <PackageReference Include="NewLife.XCode" Version="9.7.6703.38907" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup> </ItemGroup>
+10 -1
View File
@@ -9,7 +9,7 @@
<PackageReference Include="Microsoft.AspNetCore.Session" Version="2.0.3" /> <PackageReference Include="Microsoft.AspNetCore.Session" Version="2.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.4" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.4" />
<PackageReference Include="MySqlConnector" Version="0.40.3" /> <PackageReference Include="MySqlConnector" Version="0.40.3" />
<PackageReference Include="NewLife.XCode" Version="9.7.6701.38516" /> <PackageReference Include="NewLife.XCode" Version="9.7.6703.38907" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="Senparc.Weixin.MP" Version="14.12.3" /> <PackageReference Include="Senparc.Weixin.MP" Version="14.12.3" />
<PackageReference Include="Senparc.Weixin.WxOpen" Version="1.9.1" /> <PackageReference Include="Senparc.Weixin.WxOpen" Version="1.9.1" />
@@ -273,4 +273,13 @@
<None Include="wwwroot\editor\ckeditor\skins\moono-lisa\readme.md" /> <None Include="wwwroot\editor\ckeditor\skins\moono-lisa\readme.md" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Update="app.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project> </Project>
+53 -11
View File
@@ -10,11 +10,20 @@ using COMCMS.Core;
using XCode; using XCode;
using System.Text; using System.Text;
using System.IO; using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
namespace COMCMS.Web.Controllers namespace COMCMS.Web.Controllers
{ {
public class HomeController : Controller 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() public IActionResult Index()
{ {
return View(); return View();
@@ -32,12 +41,12 @@ namespace COMCMS.Web.Controllers
public IActionResult Contact() public IActionResult Contact()
{ {
ViewData["Message"] = "Your contact page."; ViewData["Message"] = "Your contact page.";
Config cfg = Config.FindById(1); string prekey = Utils.PrefixKey;
ViewBag.siteName = cfg.SiteName; Article a = Article.FindById(1);
ViewBag.siteName = a.Title;
ViewBag.c = CookiesHelper.GetCookie("abc"); ViewBag.c = CookiesHelper.GetCookie("abc");
ViewBag.d = Utils.GetServerUrl(); ViewBag.d = prekey;
return View(); return View();
} }
@@ -49,15 +58,48 @@ namespace COMCMS.Web.Controllers
#region 测试 #region 测试
public IActionResult Test() public IActionResult Test()
{ {
JsonTip json = new JsonTip(); var css = Configuration.GetSection("connectionStrings");
string strJson = JsonTip.GetJsonString(json); var list = css.GetChildren();
if(list !=null && list.Count() > 0)
{
//Byte[] bytes = Encoding.UTF8.GetBytes(strJson); }
//Stream stream = new MemoryStream(bytes); string s = "";
//MyHttpContext.Current.Response.ContentType = "text/plain"; foreach (var item in list)
//MyHttpContext.Current.Response.Body = stream; {
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 #endregion
} }
+12 -1
View File
@@ -8,6 +8,17 @@
"SystemSetting": { "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", "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", "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"
}
} }
} }