ASP.Core set global configuration information items json

一、appsettings.json

1. Add data

 

 

2. Create a reception class

namespace FourTh.BaseModel
{
    public class ThreeOption
    {
        public int BoldDepartmentElp { get; set; }
    }
}

3. CI conduit registration information (red section) in Startup.cs

public class Startup
    {

        private readonly IConfiguration _configuration;

        public Startup(IConfiguration configuration)
        {
            _configuration = configuration;
            var three = _configuration["Three:BoldDepartmentElp"];
        }

        // 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)
        {
            services.AddMvc();
            services.AddSingleton<IEmployee, IEmployeeManager>();
            var num = _configuration.GetSection("Three");
            services.Configure<ThreeOption>(_configuration.GetSection("Three"));
        }

        // 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.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            app.UseStaticFiles();


        }
    }

4. Register (e.g. Controller, red part) in various parts of the file

    public class HomeController:Controller
    {
        private readonly IEmployee _employee;
        private readonly IOptions<ThreeOption> _threeOption;

        public HomeController(IEmployee employee,IOptions<ThreeOption> threeOption)
        {
            _employee = employee;
            _threeOption = threeOption;
        }

        public async Task<IActionResult> Index()
        {
            var result = await _employee.GetAllTask();
            return View(result);
        }


    }

 

The pages can then be used in the above

@using FourTh.BaseModel
@using FourTh.Model
@using Microsoft.Extensions.Options
@model IEnumerable<Employee>
@inject IOptions<ThreeOption> options


<h1>@options.Value.BoldDepartmentElp</h1>
<table>
    <tr>
        <td>编码</td>
        <td>姓名</td>
        <td>年龄</td>
        <td>性别</td>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.Id</td>
            <td>@item.Name</td>
            <td>@item.Age</td>
            <td>@item.Sex</td>
        </tr>

    }
</table>

 

Results are shown below:

 

 

 

 

Second, the custom json

1. Create a custom amJson.json

{
  "Three": {
    "BoldDepartmentElp": 50
  }
}

 

2. Modify Program.cs configuration (red portion)

namespace FourTh
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((context, configBuilder) =>
                {
                    configBuilder.Sources.Clear();
                    configBuilder.AddJsonFile("amJson.json");
                })
                .UseStartup<Startup>();
    }
}

 

Invalid setting value appsettings.json previous results, the effect shown below:

 

 

Thanks: https: //www.bilibili.com/video/av65313713/ p = 6?

 

Guess you like

Origin www.cnblogs.com/dzw159/p/11669065.html