There has been several changes on how to do configuration in MVC 6 projects, compared to the previous project types. We have found a bit more troublesome to do is to add custom configuration files to a project. This is mainly due to the fact that the development team of ASP.NET 5 have changed some namespace names. You will find in the old tutorial using Microsoft.Framework.ConfigurationModel will no longer be applicable. In this post, we will try to explain how to add custom configuration files to MVC 6 project, and use content from that code in code with the new namespace, Microsoft.Framework.Configuration.
How To Add Costum Configuration Files to MVC 6 Project
Step 1
First, you need to add a custom configuration file to your project. Right click on your project and select add new item then choose ASP.NET configuration file.This will add a new config.json-file to your project root.
The file will have a connection string as default, and you can add your own configuration by normal json syntax.
Step 2
To get the new configuration to support Json, you must add a dependency to the project.json file that is already existing in your project.
1 2 3 4 5 6 7 8 |
"dependencies": { "Microsoft.AspNet.Mvc": "6.0.0-beta5", "Microsoft.AspNet.Server.IIS": "1.0.0-beta5", "Microsoft.AspNet.Server.WebListener": "1.0.0-beta5", "Microsoft.AspNet.StaticFiles": "1.0.0-beta5", "Microsoft.Owin": "3.0.1", "Microsoft.Framework.Configuration.Json": "1.0.0-beta5" }, |
The other change you must do is in your startup.cs file.
1 2 3 4 5 6 7 8 |
public IConfiguration Configuration { get; set; } public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv) { //// Setup configuration sources. var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath).AddJsonFile("config.json") .AddEnvironmentVariables(); Configuration = configurationBuilder.Build(); } |
And now you are ready to use the config.json file to set configuration option that you can use in your controllers.
Step 3
If you want to make use of this configuration in an controller, you can use Dependency Injection. Easy explained, in your Startup.cs file:
1 2 3 4 5 6 7 8 9 10 |
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); // Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers. // You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json. // services.AddWebApiConventions(); services.AddSingleton(_ =>Configuration); } |
And in your Controllers, you need to change your constructor, and add an class variable:
1 2 3 4 5 |
private IConfiguration Configuration { get; set; } public ValuesController(IConfiguration Configuration) { this.Configuration = Configuration; } |
Then, you can access your custom configuration by using the Configuration variable:
1 |
Configuration["data:DefaultConnection:ConnectionString"]; |
For more information about Dependency Injection, check out this blogpost.
Looking for ASP.NET Hosting on European Server? We can help you a lot!
hostforlifeasp.net is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.