1.Startup.cs 下代碼html
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; namespace OptionsBindSample { public class Startup { /// <summary> /// 添加構造方法用來獲取配置信息 /// </summary> public IConfiguration Configuration { get; set; } public Startup(IConfiguration configuration) { Configuration = configuration; } // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { //把配置信息注入到Configure中 services.Configure<Class>(Configuration); //添加MVC,依賴注入的配置 services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } //啓動默認路由 app.UseMvcWithDefaultRoute(); //app.Run(async (context) => //{ // var myClass = new Class(); // Configuration.Bind(myClass);//綁定基礎信息 // //輸出json配置到頁面 // await context.Response.WriteAsync($"ClassNo {myClass.ClassNo}"); // await context.Response.WriteAsync($"ClassDesc {myClass.ClassDesc}"); // await context.Response.WriteAsync($"myClass Count {myClass.Students.Count}"); //}); } } }
2.建立HomeController添加如下代碼json
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; namespace OptionsBindSample.Controllers { public class HomeController : Controller { private readonly Class _myClass; public HomeController(IOptions<Class> options) { _myClass = options.Value; } public IActionResult Index() { return View(_myClass); } } }
3.視圖層展現參數app
@model OptionsBindSample.Class @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <h3>Class No: @Model.ClassNo</h3> <h3>ClassDesc: @Model.ClassDesc</h3> <div> @foreach (var item in Model.Students) { <span>Name:@item.Name</span> <span>Name:@item.Age</span> } </div> </body> </html>
頁面直接經過依賴注入讀取配置信息async
@using Microsoft.Extensions.Options; @*@model OptionsBindSample.Class*@ @inject IOptions< OptionsBindSample.Class> MyClass @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <h3>Class No: @MyClass.Value.ClassNo</h3> <h3>ClassDesc: @MyClass.Value.ClassDesc</h3> <div> @foreach (var item in MyClass.Value.Students) { <span>Name:@item.Name</span> <span>Name:@item.Age</span> } </div> </body> </html>
5.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; } } }
6.json文件命名爲 appsettings.jsonui
{ "ClassNo": "1", "ClassDesc": "ASP.NET Core 101", "Students": [ { "name": "name1", "age": "12" }, { "name": "name2", "age": "13" }, { "name": "name13", "age": "14" } ] }
7. 配置的熱更新(解決了不須要重啓網站及時讀取配置信息的時候可使用)@inject IOptionsSnapshot<Class> MyClassthis
@using Microsoft.Extensions.Options; @using OptionBindSample @*@model OptionsBindSample.Class*@ @inject IOptionsSnapshot<Class> MyClass @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <h3>Class No: @MyClass.Value.ClassNo</h3> <h3>ClassDesc: @MyClass.Value.ClassDesc</h3> <div> @foreach (var item in MyClass.Value.Students) { <span>Name:@item.Name</span> <span>Name:@item.Age</span> } </div> </body> </html>
重寫ConfigureAppConfiguration能夠關閉JSON熱更新spa
public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration(option => { option.AddJsonFile("appsettings.json", false, false) }) .UseStartup<Startup>(); }