日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET Core appsettings.json文件(9)《从零开始学ASP.NET CORE MVC》:

發布時間:2023/12/4 asp.net 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET Core appsettings.json文件(9)《从零开始学ASP.NET CORE MVC》: 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


本文出自《從零開始學ASP.NET CORE MVC》
推薦文章:ASP.NET Core launchsettings.json文件

ASP.NET Core?appsettings.json文件


在本視頻中,我們將討論ASP.NET Core 項目中appsettings.json文件的重要性。

在以前的ASP.NET版本中,我們將應用程序配置設置(例如數據庫連接字符串)存儲在web.config文件中。
在?Asp.Net?Core?中,?應用程序配置設置可以來自以下不同的配置源。

  • 文件(appsettings.json,?appsettings.{Environment}.json)?Environment環境不同,托管在對應環境。

  • User secrets (用戶機密)

  • Environment variables (環境變量)

  • Command-line arguments (命令行參數)

appsettings.json f文件:?我們的項目是通過Asp.net?Core?預制的"空"模板創建的,所以我們的項目中已經有一個appsettings.json?的文件了。
我們可以對文件進行如下修改,補充一個MyKey的鍵值對:

{
"Logging":?{
"LogLevel":?{
"Default":?"Warning"
}
},
"AllowedHosts":?"*",
"MyKey":?" appsettings.json中Mykey的值",
}

訪問配置信息

若要訪問?"Startup "?類中的配置信息, 請注入框架提供的?IConfiguration服務。Startup類位于?startup.?cs?文件中。

public class Startup
{

private IConfiguration _configuration;

// 注意,我們在這里使用了依賴注入
public Startup(IConfiguration configuration)
{
_configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.Run(async (context) =>
{
await context.Response.WriteAsync(_configuration["MyKey"]);
});
}
}

依賴注入

在以前版本的ASP.NET中,依賴注入是可選的,要配置它,我們必須使用像Ninject,autofac、castle?windsor等第三方框架。

在?asp.?net?Core?中,?依賴注入是不可或缺的一部分。依賴注入能使我們能夠創建低耦合、可擴展且易于測試的系統。

我們將在即將推出的視頻中詳細討論依賴注入,盡情期待。

ASP.NET Core IConfiguration 服務

  • IConfiguration?服務是為了從asp.net?Core?中的所有各種配置源讀取配置信息而設計的。

  • 如果在多個配置源中具有相同密鑰名稱的配置設置,簡單來說就是重名了,則后面的配置源將覆蓋先前的配置源?。

幾個地方的演示,分別是如何替換的。
launchsetting

  • 靜態類WebHost的CreateDefaultBuilder()方法在應用程序啟動時會自動去調用,按特定順序讀取配置源。

  • 要查看配置源的讀取順序,請查看以下鏈接上的ConfigureAppConfiguration()方法
    https://github.com/aspnet/MetaPackages/blob/release/2.2/src/Microsoft.AspNetCore/WebHost.cs

檢查文件后,您將看到,以下是讀取各種配置源的默認順序

  • appsettings.json,

  • appsettings.{Environment}.json

  • 用戶機密

  • 環境變量
    5.命令行參數

  • 如果您想要改變他們的調用順序,甚至往里面添加屬于自己的自定義配置信息,我們將在后面的課程中討論如何自定義配置源。

    小結

    所以翻源代碼也沒有那么可怕嘛

    /// <summary>
    /// Initializes a new instance of the <see cref="WebHostBuilder"/> class with pre-configured defaults using typed Startup.
    /// </summary>
    /// <remarks>
    /// The following defaults are applied to the returned <see cref="WebHostBuilder"/>:
    /// use Kestrel as the web server and configure it using the application's configuration providers,
    /// set the <see cref="IHostingEnvironment.ContentRootPath"/> to the result of <see cref="Directory.GetCurrentDirectory()"/>,
    /// load <see cref="IConfiguration"/> from 'appsettings.json' and 'appsettings.[<see cref="IHostingEnvironment.EnvironmentName"/>].json',
    /// load <see cref="IConfiguration"/> from User Secrets when <see cref="IHostingEnvironment.EnvironmentName"/> is 'Development' using the entry assembly,
    /// load <see cref="IConfiguration"/> from environment variables,
    /// load <see cref="IConfiguration"/> from supplied command line args,
    /// configure the <see cref="ILoggerFactory"/> to log to the console and debug output,
    /// enable IIS integration.
    /// </remarks>
    /// <typeparam name ="TStartup">The type containing the startup methods for the application.</typeparam>
    /// <param name="args">The command line args.</param>
    /// <returns>The initialized <see cref="IWebHostBuilder"/>.</returns>
    public static IWebHostBuilder CreateDefaultBuilder<TStartup>(string[]?args)?where?TStartup?:?class?=>
    CreateDefaultBuilder(args).UseStartup<TStartup>();





    硬廣專區



    如果您覺得我的文章質量還不錯,歡迎打賞,也可以訂閱我的視頻哦

    未得到授權不得擅自轉載本文內容,52abp.com保留版權

    文字版目錄:?https://www.52abp.com/Wiki/mvc/latest/1.Intro?

    代碼托管地址:https://gitee.com/aiabpedu
    知乎專欄:https://zhuanlan.zhihu.com/52abp

    交流QQ群:952387474《微軟MVP帶你學ASP.NET?CORE》
    【收費】騰訊課堂:?https://ke.qq.com/course/392589?tuin=2522cdf3?
    【免費】youtube視頻專區:http://t.cn/Ei0F2EB?
    【免費】B站:?https://space.bilibili.com/2954671?
    免費的更新慢,收費的更新快,僅此而已。就這樣。
    ?


    「好看」的人都【在看】↓↓↓





    總結

    以上是生活随笔為你收集整理的ASP.NET Core appsettings.json文件(9)《从零开始学ASP.NET CORE MVC》:的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。