本文内容如下:
一 .Net core Web项目获取Json配置文件
二 .Net framework Web项目获取Config配置文件
三. .Net framework 控制台程序获取Config配置文件
一 .Net core Web项目获取Json配置文件
appsettings.json文件内容如下:
{ "ConnectionStrings": { "DefaultConnection": "User ID=aaa;Password=bbb;Data Source=ccc;Initial Catalog=acc_inventory;Persist Security Info=True;" }, "AppSettings": { "StorageConnectionstring": "DefaultEndpointsProtocol=https;AccountName=accazchannel;AccountKey=ddddddddddddd;EndpointSuffix=eeeeeeeeeeee", "ContainerName": "test" }, "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*", "UseRowNumberForPaging": true }
方式一:
1. 建立CS文件ConModel.cs,添加如下代码
public class ConModel { public static string DefaultConnection { get; set; } public static string StorageConnectionstring { get; set; } public static string ContainerName { get; set; } }
2. 在Startup.cs文件中,在ConfigureServices中添加如下代码
public void ConfigureServices(IServiceCollection services) {
//为ConModel中的变量赋值 var DefaultConnection = Configuration.GetConnectionString("DefaultConnection"); var StorageConnectionstring = Configuration.GetSection("AppSettings")["StorageConnectionstring"]; var ContainerName = Configuration.GetSection("AppSettings")["ContainerName"]; ConModel.DefaultConnection = DefaultConnection; ConModel.StorageConnectionstring = StorageConnectionstring; ConModel.ContainerName = ContainerName; services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }
3. 通过ConModel.DefaultConnection, ConModel.StorageConnectionstring, ConModel.ContainerName即可获得Json文件中的相关内容
方式二:
1. 新建AppSettings.cs文件,添加如下代码
public class AppSettings { public string StorageConnectionstring { get; set; } public string ContainerName { get; set; } }
2. 在Startup.cs文件中,在ConfigureServices中添加如下代码
public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); // 将config里的参数加到AppSettings文件中 services.AddOptions(); services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
services.Configure<AppSettings>(Configuration.GetSection("ConnectionStrings"));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }
3. 添加引用 using Microsoft.Extensions.Options;
在每个Controller中添加如下代码即可
public class DefaultController : Controller
{
private static string DefaultConnection { get; set; }
private static string StorageConnectionstring { get; set; }
private static string containerName { get; set; }
public DefaultController(IOptions<AppSettings> setting)
{
DefaultConnection= setting.Value.DefaultConnection;
StorageConnectionstring = setting.Value.StorageConnectionstring;
containerName = setting.Value.ContainerName;
}
public IActionResult Index()
{
string def = DefaultConnection;
string sto = StorageConnectionstring;
string con = containerName;
return View();
}
}
二 .Net framework Web项目获取Config配置文件
config文件如下:
<configuration> <connectionStrings> <add name="TestConnectionString" connectionString="data source=aaaaaaaaaa;initial catalog=ACC_CBDChannel_V2;user id=cbd;pwd=windows2)))" providerName="System.Data.SqlClient" /> </connectionStrings> <appSettings> <add key="ReportUser" value="king2_chen,mxk668_meng" /> </appSettings> </configuration>
获取相关数据代码:
添加引用:using System.Configuration;
private static string TestConnectionString = ConfigurationManager.ConnectionStrings["TestConnectionString"].ToString(); private static string ReportUser = ConfigurationManager.AppSettings["ReportUser"].ToString();
三. .Net framework 控制台程序获取Config配置文件
Config文件如下:
<appSettings> <add key="DefaultConnection" value="User ID=acc_inventory;Password=aaaaaaaaaaaaa;Data Source=abbbbbbbbbbbbbbb;Initial Catalog=acc_inventory;Persist Security Info=True;" /> </appSettings>
获取相关数据代码如下:
添加引用:using System.Configuration;
string DefaultConnection=ConfigurationSettings.AppSettings["DefaultConnection"];