ASP.NET 5 Configuration and IOptions

ASP.NET 5 Configuration and IOptions

ASP.NET 5 configuration is no longer about System.Configuration and XML configuration files like web.config. JSON seems to be the preferred file format based upon the fact that an ASP.NET 5 Web Application Template in Visual Studio 2015 uses JSON for most of its configuration information: appsettings.json, bower.json, package.json, project.json, etc. There are other built-in providers which I will mention, but in this ASP.NET 5 tutorial I want to show an example of using a JSON file for configuration and how to use it with dependency injection along with IOptions<T>.

ASP.NET 5 Configuration

As mentioned, ASP.NET 5 has really opened up configuration for your web applications. JSON files are quite abundant when you create a new Visual Studio 2015 ASP.NET 5 Web Application. If you open up the Startup.cs file you will see a snippet of code that uses appsettings.json as the preferred source of configuration. I removed some pieces of code for brevity.

public Startup(IHostingEnvironment env, IApplicationEnvironment app) {
  // Setup configuration sources.
  var builder = new ConfigurationBuilder()
    .SetBasePath(app.ApplicationBasePath)
    .AddJsonFile("appsettings.json");

  Configuration = builder.Build();
}

To get this to work one needs to add a couple of dependencies in project.json just in case you're doing this in an empty ASP.NET 5 Template.

"Microsoft.Framework.Configuration.Abstractions": "...",
"Microsoft.Framework.Configuration.Json": "..."

As you can see, configuration now occurs in the constructor of the Startup Class and we have a new ConfigurationBuilder Class as well as extension methods (e.g. AddJsonFile ) that help populate the configuration from various files. There are built-in providers for xml, ini, json, environment variables, etc.

Appsettings.json and Site Class

Let's add some site settings to the appsettings.json file. I will keep it simple and just add the website name, which I will inject into a view later ( as a Site object ) to display on an ASP.NET MVC 6 View. Here is the appsettings.json file:

{
  "Site": {
    "Name": "My Website"
  }
}

Here is a class that will hold the site settings. For simplicity I kept the names and properties the same.

public class Site {
    public string Name { get; set; }
}

ASP.NET 5 Dependency Injection

I want to be able to inject the site settings as a dependency into various controllers, services, and views. Dependencies get added in the ConfigureServices method of Startup, and so I will add these dependencies using some ASP.NET 5 wizardry. For more information on dependency injection in ASP.NET 5, see my blog post: ASP.NET MVC 6 Dependency Injection.

public void ConfigureServices(IServiceCollection services) {
    services.AddOptions();
    services.Configure<Site>(Configuration.GetSection("Site"));
}

Lots of good stuff happening here. To make a long story short, these two lines wire up the ability to inject IOptions<Site> into various controllers, services, and views in our ASP.NET 5 web application. The Site section in our appsettings.json file and Site Class are bound and available for injection as IOptions<T>, where T is Site in this case. Let's see this in action.

Injecting IOptions<T> into ASP.NET MVC 6 Controller

One option is to inject these settings right into a controller or service. Here is an example.

public class TestController : Controller {
    IOptions<Site> _siteSettings;

    public TestController(IOptions<Site> siteSettings) {
        _siteSettings = siteSettings;
    }

    public IActionResult Index() {
        return View(_siteSettings);
    }
}

Injecting IOptions<T> into ASP.NET MVC 6 View

If we are only using those settings in a view, there is no reason to inject it into the controller and then pass it to the view. Just inject the dependency directly into the view. For more information on dependency injection into ASP.NET MVC 6 Views, see my blog post: ASP.NET MVC 6 View Injection using Inject.

@inject IOptions<Site> site
<p>Site Name: @site.Value.Name</p>

Conclusion

I really like how one can read configuration from many sources in ASP.NET 5 and how JSON has become the preferred source. More importantly, however, I love how you can inject the configuration information into your IoC container, bind it to a particular class, and then inject that class into your controllers, services, views, etc. The various extension methods and IOptions<T> make this incredibly simple for my ASP.NET MVC 6 Web Applications.