ASP.Net Core 5.0 MVC AppSettings配置文件讀取,Startup 類中ConfigureServices 方法、Configure 方法的使用

配置文件讀取

1. 新建FirstController控制器json

  在appsettings文件內容替換成如下代碼api

{
  "Position": {
    "Title": "EditTool For human",
    "Name": "Joe Smith"
  },//json對象
  "MyKey": "My appsettings.json Value",
  "StudentList": [
    {"sName": "Jack"},
    {"sName":"John"}
  ],//json數組
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }//json對象嵌套
  },
  "AllowedHosts": "*"
}

配置文件讀取

在Fristcontroller添加Index方法,複製如下內容數組

        public IConfiguration Configuration { get; }
        //構造函數注入 configuration
        public FirstController(IConfiguration configuration)
        {
            Configuration = configuration;
           

        }
        
        public IActionResult Index()
        {
            //配置文件的讀取
            ViewBag.Title = Configuration["Position:Title"];//json對象
            ViewBag.MyKey = Configuration["MyKey"];
            ViewBag.sName1 = Configuration["StudentList:0:sName"];//json數組
            ViewBag.sName2 = Configuration["StudentList:1:sName"];
            ViewBag.Default = Configuration["Logging:LogLevel:Default"];//json嵌套對象
            return View();
        }

新增index視圖,複製如下內容安全

@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<p>  ViewBag.Title 的值:    @ViewBag.Title</p>
<p>  ViewBag.MyKey的值:    @ViewBag.MyKey</p>
<p>  ViewBag.sName1的值:     @ViewBag.sName1</p>
<p>  ViewBag.sName2的值:   @ViewBag.sName2</p>
<p>  ViewBag.Default的值:    @ViewBag.Default</p>

運行測試效果mvc

 

Startup 類

ASP.NET Core 應用使用 Startup 類,按照約定命名爲 Startup。 Startup 類:app

在應用啓動時,ASP.NET Core 運行時會調用 ConfigureServices 和 Configure函數

ConfigureServices 方法

ConfigureServices 方法:測試

  • 可選。
  • 在 Configure 方法配置應用服務以前,由主機調用。
  • 其中按常規設置配置選項

主機可能會在調用 Startup 方法以前配置某些服務。 有關詳細信息,請參閱主機ui

對於須要大量設置的功能,IServiceCollection 上有 Add{Service} 擴展方法。 例如,AddDbContext、AddDefaultIdentity、AddEntityFrameworkStores 和 AddRazorPages :spa

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>(
            options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();

        services.AddRazorPages();
    }

將服務添加到服務容器,使其在應用和 Configure 方法中可用。 服務經過依賴關係注入或 ApplicationServices 進行解析。

Configure 方法

Configure 方法用於指定應用響應 HTTP 請求的方式。 可經過將中間件組件添加到 IApplicationBuilder 實例來配置請求管道。 Configure 方法可以使用 IApplicationBuilder,但未在服務容器中註冊。 託管建立 IApplicationBuilder 並將其直接傳遞到 Configure

ASP.NET Core 模板配置的管道支持:

   public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });
    }

每一個 Use 擴展方法將一個或多箇中間件組件添加到請求管道。 例如,UseStaticFiles 配置中間件提供靜態文件

請求管道中的每一箇中間件組件負責調用管道中的下一個組件,或在適當狀況下使鏈發生短路。

能夠在 Configure 方法簽名中指定其餘服務,如 IWebHostEnvironmentILoggerFactory 或 ConfigureServices 中定義的任何內容。 若是這些服務可用,則會被注入。

相關文章
相關標籤/搜索