国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

ASP.NET Core 3.1系列(4)——讀寫配置文件appsettings.json

這篇具有很好參考價值的文章主要介紹了ASP.NET Core 3.1系列(4)——讀寫配置文件appsettings.json。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

1、前言

在大多數的應用程序中,一些參數需要寫在配置文件里,以此增加系統(tǒng)的靈活性。在ASP.NET時代,配置參數一般會寫在web.config文件中,其本質上是對XML文件的讀取和寫入。而在ASP.NET Core中,配置文件變成了appsettings.json文件。相較于XML,JSON文件更加輕量且靈活,下面就來介紹一下如何在ASP.NET Core中對其進行讀寫操作。

2、添加配置參數

打開appsettings.json文件,添加如下配置項。如果對JSON文件的格式不熟悉,建議先了解一下其格式規(guī)范,其代碼如下所示:

{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
        }
    },
    "AllowedHosts": "*",
    "ConnectionStrings": {
        "ConnectionString": "Data Source=DSF-PC;Initial Catalog=Dao;User ID=sa;Password=123456"
    },
    "AppName": "ASP.NET Core API",   // 應用程序名稱
    "Author": "HerryDong",           // 作者
    "AppSettings": {
        "DefaultLanguage": "zh-CN",  // 默認語言
        "MinLevel": 10,              // 地圖最小層級
        "MaxLevel": 16,              // 地圖最大層級
        "Center": {
            "Longitude": 120,        // 地圖中心點經度
            "Latitude": 30           // 地圖中心點維度
        }
    }
}

3、基于配置節(jié)點名稱的讀取

3.1、注入IConfiguration接口

ASP.NET Core中,如果希望通過配置項的名稱獲取配置參數,那就需要用到IConfiguration接口。該接口已經默認存在于IoC容器中,無需手動注冊。創(chuàng)建一個新的控制器HomeController,在構造函數中注入IConfiguration接口,代碼如下所示:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace App.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        private readonly IConfiguration configuration;

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="configuration"></param>
        public HomeController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }
    }
}

3.2、讀取配置參數

HomeController中添加一個Get方法,添加如下代碼:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace App.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        private readonly IConfiguration configuration;

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="configuration"></param>
        public HomeController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        /// <summary>
        /// 讀取配置參數
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string Get()
        {
            string connectionString = configuration.GetConnectionString("ConnectionString");
            string appName = configuration["AppName"];
            string author = configuration["Author"];
            string defaultLanguage = configuration["AppSettings:DefaultLanguage"];
            string minLevel = configuration["AppSettings:MinLevel"];
            string maxLevel = configuration["AppSettings:MaxLevel"];
            string longitude = configuration["AppSettings:Center:Longitude"];
            string latitude = configuration["AppSettings:Center:Latitude"];
            return connectionString + "\n" + appName + "\n" + author + "\n" +
                   defaultLanguage + "\n" + minLevel + "\n" + maxLevel + "\n" +
                   longitude + "\n" + latitude;
        }
    }
}
3.2.1、數據庫連接字符串

對于數據庫連接字符串ConnectionString,可以直接通過IConfiguration接口的GetConnectionString方法進行獲取:

string connectionString = configuration.GetConnectionString("ConnectionString");
3.2.2、一級子節(jié)點

由于AppName、Author位于一級節(jié)點,因此可通過如下方式進行獲?。?/p>

string appName = configuration["AppName"];
string author = configuration["Author"];
3.2.3、二級子節(jié)點

二級節(jié)點DefaultLanguage、MinLevel、MaxLevel位于一級節(jié)點AppSettings下,因此需要通過添加一個冒號:的方式進行獲取:

string defaultLanguage = configuration["AppSettings:DefaultLanguage"];
string minLevel = configuration["AppSettings:MinLevel"];
string maxLevel = configuration["AppSettings:MaxLevel"];
3.2.4、三級子節(jié)點

三級節(jié)點LongitudeLatitude位于二級節(jié)點Center下,因此需要通過添加兩個冒號::的方式進行獲取:

string longitude = configuration["AppSettings:Center:Longitude"];
string Latitude = configuration["AppSettings:Center:Latitude"];

通過配置節(jié)點名稱獲取配置參數的方法比較直觀,在寫法上只需要注意配置節(jié)點所在的層級即可,程序運行結果如下圖所示:

ASP.NET Core 3.1系列(4)——讀寫配置文件appsettings.json

4、基于配置文件實體類的讀取

4.1、構建配置文件實體類

ASP.NET Core也允許通過配置文件實體類的方式讀取配置文件。首先根據appsettings.json中各個節(jié)點的層級關系構建相應實體類ConfigModel,其代碼如下所示:

namespace App
{
    public class ConfigModel
    {
        public ConnectionStrings ConnectionStrings { get; set; }
        public string AppName { get; set; }
        public string Author { get; set; }
        public AppSettings AppSettings { get; set; }
    }

    public class ConnectionStrings
    {
        public string ConnectionString { get; set; }
    }

    public class AppSettings
    {
        public string DefaultLanguage { get; set; }
        public int MinLevel { get; set; }
        public int MaxLevel { get; set; }
        public Center Center { get; set; }
    }

    public class Center
    {
        public double Longitude { get; set; }
        public double Latitude { get; set; }
    }
}

4.2、注冊配置文件實體類

在構建完配置文件實體類后,我們需要在Startup.cs文件中對其進行注冊,調用services.Configure方法即可,代碼如下所示:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace App
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            // 注冊配置文件實體類 
            services.Configure<ConfigModel>(Configuration);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

4.3、注入IOptions接口

這里需要注意,原先通過配置節(jié)點名稱讀取配置參數值的時候,我們注入的是IConfiguration接口,而這里則需要換成IOptions接口,最后通過IOptions.Value獲取實體類。由于IOptions接口默認存在于IoC容器中,因此無需手動對其進行注冊。其代碼如下所示:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace App.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        private readonly ConfigModel model;

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="options"></param>
        public HomeController(IOptions<ConfigModel> options)
        {
            this.model = options.Value;
        }
    }
}

4.4、讀取配置參數

現在我們已經獲取了配置文件實體類,接下來的工作很簡單了,由于是強類型操作,Visual Studio 2019會幫助你自動感知配置項,其代碼如下所示:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace App.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        private readonly ConfigModel model;

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="options"></param>
        public HomeController(IOptions<ConfigModel> options)
        {
            this.model = options.Value;
        }

        /// <summary>
        /// 讀取配置參數
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string Get()
        {
            string connectionString = model.ConnectionStrings.ConnectionString;
            string appName = model.AppName;
            string author = model.Author;
            string defaultLanguage = model.AppSettings.DefaultLanguage;
            int minLevel = model.AppSettings.MinLevel;
            int maxLevel = model.AppSettings.MaxLevel;
            double longitude = model.AppSettings.Center.Longitude;
            double latitude = model.AppSettings.Center.Latitude;
            return connectionString + "\n" + appName + "\n" + author + "\n" +
                   defaultLanguage + "\n" + minLevel + "\n" + maxLevel + "\n" +
                   longitude + "\n" + latitude;
        }
    }
}

IConfiguration接口讀取配置參數的方法相比,IOptions接口的優(yōu)勢是可以利用實體類進行映射和讀取,這也可以杜絕由于寫錯配置項名稱而引起錯誤的弊端。程序運行結果如下圖所示:

ASP.NET Core 3.1系列(4)——讀寫配置文件appsettings.json

5、寫入配置文件

5.1、獲取appsettings.json的路徑

其實在實際開發(fā)過程中,微軟并不推薦開發(fā)者對appsettings.json文件進行動態(tài)修改,一些需要動態(tài)配置的參數最好寫在其他文件中,但在某些特殊情況下我們還是得這么干。話說回來,既然涉及到文件修改,那我們肯定得先獲取這個文件的路徑,否則一切免談。在ASP.NET Core中,如果要獲取一個文件的路徑,我們可以使用IWebHostEnvironment接口,在使用時只需要將其注入控制器的構造函數即可,由于它已經默認存在于IoC容器中,因此我們無需進行手動注冊,代碼如下所示:

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System.IO;

namespace App.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        private readonly IWebHostEnvironment webHostEnvironment;

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="webHostEnvironment"></param>
        public HomeController(IWebHostEnvironment webHostEnvironment)
        {
            this.webHostEnvironment = webHostEnvironment;
        }

        /// <summary>
        /// 獲取文件路徑
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string Get()
        {
            string filePath = Path.Combine(webHostEnvironment.ContentRootPath, "appsettings.json");
            return filePath;
        }
    }
}

運行一下程序,發(fā)現可以正確獲取appsettings.json文件的路徑,如下圖所示:

ASP.NET Core 3.1系列(4)——讀寫配置文件appsettings.json

5.2、修改配置參數

我們可以借助Newtonsoft.Json來實現配置參數的修改,使用NuGet將其引入,如下圖所示:

ASP.NET Core 3.1系列(4)——讀寫配置文件appsettings.json

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using System.IO;

namespace App.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        private readonly IWebHostEnvironment webHostEnvironment;

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="webHostEnvironment"></param>
        public HomeController(IWebHostEnvironment webHostEnvironment)
        {
            this.webHostEnvironment = webHostEnvironment;
        }

        /// <summary>
        /// 修改配置參數
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string Get()
        {
            try
            {
                // 讀取appsettings.json文本內容
                string filePath = Path.Combine(webHostEnvironment.ContentRootPath, "appsettings.json");
                string text = System.IO.File.ReadAllText(filePath);

                // 修改配置項
                JObject obj = JObject.Parse(text);
                obj["AppName"] = "API";
                obj["Author"] = "Herry";
                obj["AppSettings"]["DefaultLanguage"] = "en-US";
                obj["AppSettings"]["MinLevel"] = 15;
                obj["AppSettings"]["MaxLevel"] = 20;
                obj["AppSettings"]["Center"]["Longitude"] = 130;
                obj["AppSettings"]["Center"]["Latitude"] = 40;

                // 重新寫入appsettings.json
                string result = obj.ToString();
                System.IO.File.WriteAllText(filePath, result);
                return "success";
            }
            catch
            {
                return "failed";
            }
        }
    }
}

運行程序,發(fā)現appsettings.json的配置項修改成功。但也可以發(fā)現一個問題:原先的注釋都沒了,因此最好不要對該文件進行動態(tài)修改。程序運行結果如下圖所示:
ASP.NET Core 3.1系列(4)——讀寫配置文件appsettings.json

5.3、注入IOptionsSnapshot接口

現在我們已經實現了appsettings.json文件的修改,但如果仍舊使用IOptions接口讀取配置參數會怎么樣呢?看下面一段代碼:

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using System.IO;

namespace App.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        private readonly IWebHostEnvironment webHostEnvironment;
        private readonly ConfigModel model;

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="webHostEnvironment"></param>
        /// <param name="options"></param>
        public HomeController(IWebHostEnvironment webHostEnvironment,
                              IOptions<ConfigModel> options)
        {
            this.webHostEnvironment = webHostEnvironment;
            this.model = options.Value;
        }

        /// <summary>
        /// 修改配置參數
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string Get()
        {
            try
            {
                // 讀取appsettings.json文本內容
                string filePath = Path.Combine(webHostEnvironment.ContentRootPath, "appsettings.json");
                string text = System.IO.File.ReadAllText(filePath);

                // 修改
                JObject obj = JObject.Parse(text);
                obj["AppName"] = "API";
                obj["Author"] = "Herry";
                obj["AppSettings"]["DefaultLanguage"] = "en-US";
                obj["AppSettings"]["MinLevel"] = 15;
                obj["AppSettings"]["MaxLevel"] = 20;
                obj["AppSettings"]["Center"]["Longitude"] = 130;
                obj["AppSettings"]["Center"]["Latitude"] = 40;

                // 重新寫入appsettings.json
                string result = obj.ToString();
                System.IO.File.WriteAllText(filePath, result);
                return "success";
            }
            catch
            {
                return "failed";
            }
        }

        /// <summary>
        /// 讀取配置參數
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string Get_2()
        {
            string connectionString = model.ConnectionStrings.ConnectionString;
            string appName = model.AppName;
            string author = model.Author;
            string defaultLanguage = model.AppSettings.DefaultLanguage;
            int minLevel = model.AppSettings.MinLevel;
            int maxLevel = model.AppSettings.MaxLevel;
            double longitude = model.AppSettings.Center.Longitude;
            double latitude = model.AppSettings.Center.Latitude;
            return connectionString + "\n" + appName + "\n" + author + "\n" +
                   defaultLanguage + "\n" + minLevel + "\n" + maxLevel + "\n" +
                   longitude + "\n" + latitude;
        }
    }
}

在執(zhí)行完Get方法后,我們再執(zhí)行Get_2方法,發(fā)現好像不太對,怎么讀出來的還是修改前的配置參數?

ASP.NET Core 3.1系列(4)——讀寫配置文件appsettings.json
其實,如果對appsettings.json文件做了修改,那么就不能使用IOptions接口去讀取配置參數了,因為該接口實例被注冊為全局單例生命周期,因此無法讀到最新的配置參數。在這種情況下,我們需要使用另一個接口來實現配置參數的讀取,那就是IOptionsSnapshot接口,由于該接口被注冊為域生命周期,因此每次http訪問都能讀取到最新配置,其代碼如下所示:

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using System.IO;

namespace App.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        private readonly IWebHostEnvironment webHostEnvironment;
        private readonly ConfigModel model;

        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="webHostEnvironment"></param>
        /// <param name="options"></param>
        public HomeController(IWebHostEnvironment webHostEnvironment,
                              IOptionsSnapshot<ConfigModel> options)
        {
            this.webHostEnvironment = webHostEnvironment;
            this.model = options.Value;
        }

        /// <summary>
        /// 修改配置參數
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string Get()
        {
            try
            {
                // 讀取appsettings.json文本內容
                string filePath = Path.Combine(webHostEnvironment.ContentRootPath, "appsettings.json");
                string text = System.IO.File.ReadAllText(filePath);

                // 修改
                JObject obj = JObject.Parse(text);
                obj["AppName"] = "API";
                obj["Author"] = "Herry";
                obj["AppSettings"]["DefaultLanguage"] = "en-US";
                obj["AppSettings"]["MinLevel"] = 15;
                obj["AppSettings"]["MaxLevel"] = 20;
                obj["AppSettings"]["Center"]["Longitude"] = 130;
                obj["AppSettings"]["Center"]["Latitude"] = 40;

                // 重新寫入appsettings.json
                string result = obj.ToString();
                System.IO.File.WriteAllText(filePath, result);
                return "success";
            }
            catch
            {
                return "failed";
            }
        }

        /// <summary>
        /// 讀取配置參數
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string Get_2()
        {
            string connectionString = model.ConnectionStrings.ConnectionString;
            string appName = model.AppName;
            string author = model.Author;
            string defaultLanguage = model.AppSettings.DefaultLanguage;
            int minLevel = model.AppSettings.MinLevel;
            int maxLevel = model.AppSettings.MaxLevel;
            double longitude = model.AppSettings.Center.Longitude;
            double latitude = model.AppSettings.Center.Latitude;
            return connectionString + "\n" + appName + "\n" + author + "\n" +
                   defaultLanguage + "\n" + minLevel + "\n" + maxLevel + "\n" +
                   longitude + "\n" + latitude;
        }
    }
}

運行程序,發(fā)現可以讀取到最新的配置參數,結果如下圖所示:

ASP.NET Core 3.1系列(4)——讀寫配置文件appsettings.json

6、結語

本文主要介紹了ASP.NET Core中配置文件的讀寫方法。在實際開發(fā)過程中,如果涉及到項目遷移,即:之前已經存在很多配置參數,我們可以使用IConfiguration接口讀取配置參數,如果是新的項目,則可以考慮使用IOptions接口讀取,因為強類型的讀取方法可以避免因配置項名稱寫錯而引發(fā)的一系列問題。文章來源地址http://www.zghlxwxcb.cn/news/detail-458088.html

到了這里,關于ASP.NET Core 3.1系列(4)——讀寫配置文件appsettings.json的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • Asp .Net Core 系列: 集成 CORS跨域配置

    Asp .Net Core 系列: 集成 CORS跨域配置

    CORS,全稱是“跨源資源共享”(Cross-Origin Resource Sharing),是一種Web應用程序的安全機制,用于控制不同源的資源之間的交互。 在Web應用程序中,CORS定義了一種機制,通過該機制,瀏覽器能夠限制哪些外部網頁可以訪問來自不同源的資源。源由協議、域名和端口組成。當一

    2024年01月24日
    瀏覽(114)
  • asp.net core在發(fā)布時排除配置文件

    使用命令發(fā)布 dotnet restore dotnet publish -c Release?-r win-x64 -o \\\"D:services\\\"? 這樣發(fā)布總是是將配置文件覆蓋? 這時候打開項目.csproj新增配置文件

    2024年04月24日
    瀏覽(16)
  • Asp.net Core系列學習(1)

    ASP.NET Core 是一個跨平臺的高性能開源 框架 ,用于生成啟用云且連接 Internet 的新式應用。 使用 ASP.NET Core,可以: 生成 Web 應用和服務、物聯網 (IoT) 應用和移動后端。 在 Windows、macOS 和 Linux 上使用喜愛的開發(fā)工具。 部署到云或本地。 在 .NET Core 上運行。 ASP.NET Core 是對 ASP

    2024年02月06日
    瀏覽(93)
  • ASP.NET Core 依賴注入系列一

    ASP.NET Core 依賴注入系列一

    什么是ASP.NET Core 依賴注入? 依賴注入也稱DI是一項技術用來實現對象松耦合以至于應用程序更容易維護,ASP.NET Core通過控制器的構造函數自動注入依賴的對象,我們創(chuàng)建ASP.NET Core MVC應用程序演示依賴注入特性是如何工作, 在這節(jié)中我們講解該特性 1 例子 我們創(chuàng)建一個ASP.NET C

    2024年02月11日
    瀏覽(98)
  • ASP.NET Core SignalR 系列(二)- 中心(服務端)

    ASP.NET Core SignalR 系列(二)- 中心(服務端)

    本章將和大家分享 ASP.NET Core SignalR 中的中心(服務端)。 本文大部分內容摘自微軟官網:https://learn.microsoft.com/zh-cn/aspnet/core/signalr/hubs?view=aspnetcore-7.0 廢話不多說,我們直接來看一個Demo,Demo的目錄結構如下所示: 本Demo的Web項目為ASP.NET Core Web 應用程序( 目標框架為.NET 7.0

    2024年02月13日
    瀏覽(94)
  • 你所不知道的ASP.NET Core進階系列(三)

    你所不知道的ASP.NET Core進階系列(三)

    一年多沒更新博客,上一次寫此系列還是四年前,雖遲但到,沒有承諾,主打隨性,所以不存在斷更,催更,哈哈,上一篇我們細究從請求到綁定詳細原理,本篇則是探討模型綁定細節(jié),當一個問題產生到最終解決時,回過頭我們整體分析其產生背景以及設計思路才能有所獲

    2024年02月05日
    瀏覽(436)
  • ASP.NET Core SignalR 系列(四)- 中心篩選器

    本章將和大家分享 ASP.NET Core SignalR 中的中心篩選器。 本文大部分內容摘自微軟官網:https://learn.microsoft.com/zh-cn/aspnet/core/signalr/hub-filters?view=aspnetcore-7.0 廢話不多說,下面我們直接進入本章主題。 中心篩選器: 在 ASP.NET Core 5.0 或更高版本中可用。 允許在客戶端調用中心方法之

    2024年02月16日
    瀏覽(101)
  • ASP.NET Core —配置系統(tǒng)

    ASP.NET Core —配置系統(tǒng)

    一個應用要運行起來,往往需要讀取很多的預設好的配置信息,根據約定好的信息或方式執(zhí)行一定的行為。 配置的本質就是軟件運行的參數,在一個軟件實現中需要的參數非常多,如果我們以 Hard Code (硬編碼)的方式寫在應用代碼中,這樣配置就會很亂,而且后續(xù)也不容易修

    2024年02月08日
    瀏覽(20)
  • ASP.NET Core SignalR 系列(三)- JavaScript 客戶端

    ASP.NET Core SignalR 系列(三)- JavaScript 客戶端

    本章將和大家分享 ASP.NET Core SignalR 中的 JavaScript 客戶端。ASP.NET Core SignalR JavaScript 客戶端庫使開發(fā)人員能夠調用服務器端SignalR中心代碼。 本文大部分內容摘自微軟官網:https://learn.microsoft.com/zh-cn/aspnet/core/signalr/javascript-client?view=aspnetcore-7.0tabs=visual-studio 廢話不多說,下面我們

    2024年02月15日
    瀏覽(99)
  • Asp .Net Core 系列: 集成 Consul 實現 服務注冊與健康檢查

    Asp .Net Core 系列: 集成 Consul 實現 服務注冊與健康檢查

    官網:https://www.consul.io/ Consul 是一款開源的服務發(fā)現和配置管理工具,它能夠監(jiān)控應用程序和服務之間的通信,并提供了一組 API 和 Web UI,用于管理服務和配置。 Consul 是分布式的、高可用的、可橫向擴展的,具備以下特性: 服務發(fā)現:Consul 通過 DNS 或者 HTTP 接口使服務注冊

    2024年01月21日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包