ASP.NET Core 1.0 Hosting Tutorial :: How To Add Costum Configuration Files to MVC 6 Project

 

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 - hostforlife TutorialHow 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.

Add Costum Configuration Files to MVC 6 Project 1

Add Costum Configuration Files to MVC 6 Project 2The file will have a connection string as default, and you can add your own configuration by normal json syntax.

Add Costum Configuration Files to MVC 6 Project 3Step 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.

The other change you must do is in your startup.cs file.
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:

And in your Controllers, you need to change your constructor, and add an class variable:

Then, you can access your custom configuration by using the Configuration variable:

For more information about Dependency Injection, check out this blogpost.

Looking for ASP.NET Hosting on European Server? We can help you a lot!

HostForLIFE.eu 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.

hostforlifebanner

HostForLIFE.eu is Microsoft No #1 Recommended Windows and ASP.NET Hosting in European Continent. HostForLIFE.eu offers a highly redundant, carrier-class architecture, designed around the needs of shared hosting customers. The company’s prices for shared and dedicated hosting are competitive; its features and perks are robust;