iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 23
0

Introduction


We will learn the following ways to use Configuration in ASP.NET Core project.

  1. Inject to Service Container
  2. Create a static config-provider

Environment


  • Visual Studio 2015 Update 3
  • Microsoft.AspNetCore.Mvc 1.0.0

Implement


Default way

Here is the default way to read the contents in appsettings.json.

  • Startup.cs
public IConfigurationRoot Configuration { get; }

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
    Configuration = builder.Build();

    var siteName = Configuration["Config:SiteName"];
}
  • appsettings.json
{
  "Config": {
    "SiteName": "ASP.NET Core X Angular2",
    "SiteOwner":  "2016 JB"
  }
}

Inject to Service container

Create a class to store the configuration values.

  • ConfigManager.cs
public class ConfigManager
{
        public string SiteName { get; set; }
        public string SiteOwner { get; set; }
}
  • Startup.cs

Then inject the configurations as a ConfigManager object to the Service container.

public void ConfigureServices(IServiceCollection services)
{
      //Skip…

      services.Configure<ConfigManager>(Configuration.GetSection("Config"));
}

Usage in MVC Controller

private ConfigManager _configManager = null;

public HomeController(IOptions<ConfigManager> options)
{
    IOptions<ConfigManager> cfgMangerOptions = options;
  this._configManager = cfgMangerOptions.Value;
}
public IActionResult Index()
{
    ViewBag.Title = this._configManager.SiteName;
    ViewBag.Owner = this._configManager.SiteOwner;
    return View();
}

Result:

Create a static config-provider

Here is the other simple way to make the configurations available at any class, not just Controller. Just create a static class to store them.

  • ConfigProvider.cs
public static class ConfigProvider
{
        public static string SiteName { get; set; }
        public static string SiteOwner { get; set; }
}
  • Startup.cs
public void ConfigureServices(IServiceCollection services)
{
      //Skip …
      var configManager = new ConfigManager();
      Configuration.GetSection("Config").Bind(configManager);
      ConfigProvider.SiteName = configManager.SiteName;
      ConfigProvider.SiteOwner = configManager.SiteOwner;
}

Notice that we could bind the configuration values recursively into a complex class with Configuration.GetSection("Config").Bind() function.

  • Usage
ViewBag.Title = ConfigProvider.SiteName;
ViewBag.Owner = ConfigProvider.SiteOwner;

Result :

IOptionsSnapshot

Requires ASP.NET Core 1.1 or higher.

ASP.NET Core 1.1 and higher versions support reloading configuration data when the configuration file has changed. Take a look at the sample in ASP.NET Core documents.

Reference


  1. ASP.NET Core documents

上一篇
[ASP.NET Core] View Components
下一篇
[.NET Core] Unit Test with xUnit.net
系列文
Learning ASP.NET core + Angular2 31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言