這篇文章給大家分享的是有關(guān).Net Core3.0如何配置Configuration的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
主要從事網(wǎng)頁設(shè)計、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、成都響應(yīng)式網(wǎng)站建設(shè)公司、程序開發(fā)、微網(wǎng)站、微信小程序等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計、營銷、管理等多方位專業(yè)化運(yùn)作于一體,具備承接不同規(guī)模與類型的建設(shè)項(xiàng)目的能力。
準(zhǔn)備
.NET core和.NET項(xiàng)目配置上有了很大的改變,支持的也更加豐富了比如命令行,環(huán)境變量,內(nèi)存中.NET對象,設(shè)置文件等等。.NET項(xiàng)目我們常常把配置信息放到webConfig 或者appConfig中。配置相關(guān)的源碼https://github.com/aspnet/Extensions;如果打開源碼項(xiàng)目 如果遇到以下錯誤,未遇到直接跳過。
錯誤提示: error : The project file cannot be opened by the project system, because it is missing some critical imports or the referenced SDK cannot be found. Detailed Information:
解決辦法:查看本地安裝的sdk 與 global.json中制定的版本是否一致:然后修改即可
public static IHostBuilder CreateDefaultBuilder(string[] args)
{
var builder = new HostBuilder();
builder.UseContentRoot(Directory.GetCurrentDirectory());
builder.ConfigureHostConfiguration(config =>
{
config.AddEnvironmentVariables(prefix: "DOTNET_");
if (args != null)
{
config.AddCommandLine(args);
}
});
builder.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
if (env.IsDevelopment() && !string.IsNullOrEmpty(env.ApplicationName))
{
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
if (appAssembly != null)
{
config.AddUserSecrets(appAssembly, optional: true);
}
}
將文件讀入配置時,會創(chuàng)建一下唯一的分層健來保存配置值:
Logging:LogLevel:Default
Logging:LogLevel:System
Logging:LogLevel:Microsoft
Logging:LogLevel:Microsoft.Hosting.Lifetime
AllowedHosts
var jsonValue = $"AllowedHosts:{_config["AllowedHosts"]}"+ "\r\n";
jsonValue += "Logging:LogLevel:Default:" + _config.GetValue
("Logging:LogLevel:Default")+ "\r\n";
//GetSection 返回IConfigurationSection;如果未匹配到 返回null
//jsonValue += "---" + _config.GetSection("Logging:LogLevel:System");
jsonValue += "Logging:LogLevel:System:" + _config.GetSection("Logging:LogLevel:System").Value+ "\r\n\n";
var logSection = _config.GetSection("Logging:LogLevel");
var configurationSections = logSection.GetChildren();
foreach (var sections in configurationSections)
{
jsonValue += $"{sections.Path}:{sections.Value}";
jsonValue += "\r\n";
}
jsonValue += "\r\n";
輸出:
{
"AA": {
"RabbitMqHostUrl": "rabbitmq://localhost:5672",
"RabbitMqHostName": "localhost",
"RabbitMqUserName": "admin",
"RabbitMqPassword": "123"
}
}
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, config) => { config.SetBasePath(Directory.GetCurrentDirectory()); config.AddJsonFile("AAAppSettings.json", optional: true, reloadOnChange: true); })
使用bind方法綁定到新建的類上如:
public partial class AAConfig { public string RabbitMqHostUrl { get; set; } public string RabbitMqHostName { get; set; } public string RabbitMqUserName { get; set; } public string RabbitMqPassword { get; set; } }
var aaConfig = new AAConfig();_config.GetSection("AA").Bind(aaConfig);jsonValue += aaConfig.RabbitMqHostUrl + "\r\n";jsonValue += aaConfig.RabbitMqHostName + "\r\n";jsonValue += aaConfig.RabbitMqUserName + "\r\n";jsonValue += aaConfig.RabbitMqPassword + "\r\n";return jsonValue;
運(yùn)行輸出:
感謝各位的閱讀!關(guān)于“.Net Core3.0如何配置Configuration”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!