菜鳥入門【ASP.NET Core】5:命令列配置、Json檔案配置、Bind讀取配置到C#執行個體、在Core Mvc中使用Options

來源:互聯網
上載者:User

標籤:泛型   載入   style   為我   gpo   注入   依賴   microsoft   9.png   

 命令列配置我們通過vs2017建立一個控制台項目CommandLineSample

 

可以看到現在項目以來的是dotnet core framework我們需要吧asp.net core引用進來,我們可以直接添加Microsoft.AspNetCore.All 

 

安裝完成之後,我們可以通過using Microsoft.Extensions.Configuration;來進行後續的配置
static void Main(string[] args)        {            var builder = new ConfigurationBuilder()//初始化一個ConfigurationBuilder                .AddCommandLine(args);//擴充函數             var configuration = builder.Build();//拿到configuration              //查看configuration裡面有什麼            Console.WriteLine($"name:{configuration["name"]}");            Console.WriteLine($"age:{configuration["age"]}");             Console.ReadLine();        }
這時候直接執行的話不會有結果,因為我們沒有對configuration進行配置我們可以在調試裡面進行參數配置,運行結果configuration一般以索引值對的形式存在,我們如果要給他傳遞預設的參數的話,可以用一個dictionary來傳遞,然後以記憶體的形式添加進去
using System;using System.Collections.Generic;using Microsoft.Extensions.Configuration; namespace CommandLineSample{    class Program    {        static void Main(string[] args)        {            var settings = new Dictionary<string, string>            {                {"name","菜鳥起飛"},                {"age","18"}            };              var builder = new ConfigurationBuilder()//初始化一個ConfigurationBuilder                .AddInMemoryCollection(settings)//以記憶體的形式添加進來                .AddCommandLine(args);//擴充函數             var configuration = builder.Build();//拿到configuration              //查看configuration裡面有什麼            Console.WriteLine($"name:{configuration["name"]}");            Console.WriteLine($"age:{configuration["age"]}");             Console.ReadLine();        }    }}
 這個時候我們就可以將調試裡面的參數去掉了,直接用控制台啟動項目,可以看到傳遞參數和不傳遞參數會有不同的配置顯示效果  Json檔案配置 建立控制台項目JsonConfigSample我們需要吧asp.net core引用進來,我們可以直接添加Microsoft.AspNetCore.All然後建立json檔案 class.json 並產生到bin
{  "ClassNo": "1",  "ClassDesc": "菜鳥入門ASP.NET Core",   "Students": [    {      "name": "老王",      "age": "17"    },    {      "name": "老李",      "age": "16"    },    {      "name": "老牛",      "age": "17"    }  ]}

 

然後讀取class.json檔案,讀取結果 

 

Bind讀取配置到C#執行個體 建立控制台項目OptionsBindSample

 

選擇空的模板這個時候建立的Startup.cs檔案中沒有Configuration,由於我們要用到Configuration,所以我們要用到依賴注入,把Configuration加進來這個時候我們就可以在Startup.cs中使用Configuration了建立Class.class班級類
using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks; namespace OptionsBindSample{    public class Class    {        public int ClassNo { get; set; }         public string ClassDesc { get; set; }         public List<Student> Students { get; set; }    }     public class Student    {        public string Name { get; set; }         public string Age { get; set; }    }}
添加asp.net設定檔appsettings.json
{  "ClassNo": "1",  "ClassDesc": "菜鳥入門ASP.NET Core",   "Students": [    {      "name": "老王",      "age": "17"    },    {      "name": "老李",      "age": "16"    },    {      "name": "老牛",      "age": "17"    }  ]}
為什麼要添加設定檔呢?我們可以看:因為在WebHost啟動的時候預設是沒有添加設定檔的,但是當項目啟動的時候,我們會預設讀取appsettings.json這裡的內容到Configuration裡面去,是通過CreateDefaultBuilder這個方法把他們載入進去的 這時候我們可以在Startup.cs當中將讀取的appsetting的Configuration綁定到對應的Class執行個體化模型,並列印出來,運行後現實吧效果,顯示的格式忘調了,各位可以嘗試自己去調一調

 

 在Core Mvc中使用Options添加MVC中介軟體(Middleware)首先,我們在前面的OptionsBindSample項目中建立檔案夾:Controllers、Views。添加一個HomeController。給HomeController的Index動作添加Index視圖,接下來在Startup.cs中注釋掉以下內容,因為如果不注釋的話管道會一直被佔用,導致我們的mvc無法訪問 然後在Startup.cs的ConfigureServices方法中添加mvc接下來在Startup.cs的Configure方法中配置使用MVC預設路由

 

控制器依賴注入 通過依賴注入的方式在HomeController中注入myClass。只不過這次我們不能用bing的方法,而是用IOption<T>泛型方法將班級注入進去

 

這時候我們返回強型別視圖

 

依賴注入和視圖完成之後呢,我們要在Startup.cs的ConfigureServices方法中註冊一下Class到Options,將Configuration傳進去,這樣在HomeController調用IOptions<Class>的時候,才會能從配置中拿到Class的內容 

 

啟動之後效果 

 

【小擴充】:我們也可以把注入直接取出來,通過依賴注入架構直接在視圖中顯示出來建立控制器Home2Controller,不需要添加任何依賴注入的配置。建立Index視圖,直接在視圖中引用IOtions<T>以強型別視圖的方式注入進去

 

運行效果 

 

談一談 Program.cs中的BuildWebHost方法當我們建立mvc項目的時候,自動建立的Program.cs中的BuildWebHost是下面這個樣子的,無法訪問其他的*,json設定檔對其進行修改來讀取特定的*.json設定檔

 

  

菜鳥入門【ASP.NET Core】5:命令列配置、Json檔案配置、Bind讀取配置到C#執行個體、在Core Mvc中使用Options

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.