内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

.NetCore3.0如何配置Configuration

這篇文章給大家分享的是有關(guān).Net Core3.0如何配置Configuration的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

主要從事網(wǎng)頁設(shè)計、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機版網(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)驗,集策劃、開發(fā)、設(shè)計、營銷、管理等多方位專業(yè)化運作于一體,具備承接不同規(guī)模與類型的建設(shè)項目的能力。

 準備

.NET core和.NET項目配置上有了很大的改變,支持的也更加豐富了比如命令行,環(huán)境變量,內(nèi)存中.NET對象,設(shè)置文件等等。.NET項目我們常常把配置信息放到webConfig 或者appConfig中。配置相關(guān)的源碼https://github.com/aspnet/Extensions;如果打開源碼項目 如果遇到以下錯誤,未遇到直接跳過。

.Net Core3.0如何配置Configuration

錯誤提示: 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中制定的版本是否一致:然后修改即可

.Net Core3.0如何配置Configuration

開始

新建個Asp.net Core web應(yīng)用程序系統(tǒng)默認創(chuàng)建了appsettings.json ;在應(yīng)用啟動生成主機時調(diào)用CreateDefaultBuilder方法,默認會加載  appsettings.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);                    }                }
利用 GetValue,GetSection,GetChildren讀取appsettings.json 鍵值對 。我們打開appsettings.json文件:

.Net Core3.0如何配置Configuration

將文件讀入配置時,會創(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<string>("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";

輸出:

.Net Core3.0如何配置Configuration 

配置指定json文件綁定至類

新建一個json文件-AAAppSettings.json
{  "AA": {    "RabbitMqHostUrl": "rabbitmq://localhost:5672",    "RabbitMqHostName": "localhost",    "RabbitMqUserName": "admin",    "RabbitMqPassword": "123"  }}
使用  ConfigureAppConfiguratio  n方法  配  置  指定的json文件
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;

運行輸出:

.Net Core3.0如何配置Configuration

感謝各位的閱讀!關(guān)于“.Net Core3.0如何配置Configuration”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

分享名稱:.NetCore3.0如何配置Configuration
鏈接URL:http://www.rwnh.cn/article32/jjsssc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、動態(tài)網(wǎng)站、云服務(wù)器、網(wǎng)站改版、定制開發(fā)、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)
恩平市| 沽源县| 万山特区| 龙川县| 通辽市| 勃利县| 林西县| 常州市| 尼玛县| 新绛县| 邹城市| 南丹县| 德江县| 涟源市| 鹤壁市| 旌德县| 车致| 肥乡县| 石屏县| 丁青县| 平利县| 镇原县| 开平市| 德昌县| 洱源县| 中宁县| 馆陶县| 大荔县| 慈溪市| 昭觉县| 宝清县| 凉城县| 漳平市| 汝城县| 金坛市| 班戈县| 长汀县| 巩留县| 城口县| 固始县| 剑阁县|