Some configurations like the connection string usually change on every environment your application is running on. Instead of hard-coding this information into your application, ASP.NET Core enables you to provide configuration data through different sources such as environment variables, command-line arguments or JSON files. You can find the source code for this demo on GitHub.
On the following screenshot, I configure my application in the BuildWebHost method of the program class to read the appsettings.json file, the environment variables and if available the command line arguments.
Configure the application to read settings from a JSON file, environment variables and the command line parameter
Appsettings.json is the conventional name for the configuration file but you can choose any name you like. The two additional parameters mark the file as optional and enable reloading when the file changes. This enables you to change the configuration during runtime without the need of restarting the web server like you had to do when you changed, for example, the web.config file. Only because you can change the configuration during runtime doesn’t mean that you should since it is a recipe for downtime.
The ConfigureAppConfiguration method is used to handle the configuration data and its arguments are a WebHostBuilderContext object and an IConfigurationBuilder object. The WebHostBuilderContext class has two properties:
The HostingEnvironment provides information about the hosting environment in which the application is running whereas the Configuration property provides read-only access to the configuration data.
The IConfigurationBuilder provides three extension methods:
|This method is used to load configuration data from a JSON file, such as appsettings.json.
|This method is used to load configuration data from environment variables.
|This method is used to load configuration data from the command-line arguments used to start the application.
The most common uses for the appsettings.json file are to store your connection strings and logging setting, but you can store any data that your application needs. On the following screenshot, I add the ShortCircuitMiddleware section containing EnableBrowserShortCircuit with the value true to the appsettings.json file.
In JSON everything has to be quoted exception bool and number values. If you want to add a new section, add a comma after the closing bracket of the ShortCircuitMiddleware section. Be aware to not add a trailing comma at the end if you don’t have another section there. This and missing quotes are the most common mistakes in a JSON file.
The Startup class can access the configuration data by defining a constructor with an IConfiguration argument. When the UseStartup method is called in the Program class, the configuration data prepared by the ConfigureAppConfiguration is used to create the Startup object.
The IConfiguration object is received by the constructor and assigned to a property called Configuration, which can then be used to access the configuration data that has been loaded from
environment variables, the command line, and the appsettings.json file. To obtain a value, you navigate through the structure of the data to the configuration section you require. The IConfigurationInterface defines the following member variables to do that:
|The indexer is used to obtain a string value for a specific key.
|This method returns an IConfiguration object that represents a section of the configuration data.
|This method returns an enumeration of the IConfiguration objects that represent the subsections of the current configuration object.
Additionally, the IConfiguration interface provides the following extension methods to get and convert values from string into other data types:
| This method gets the value associated with the specified key and attempts to convert it to the type T. </tr>
| This method gets the value associated with the specified key and attempts to convert it to the type T. The default value will be used if there is no value for the key in the configuration data. </tr> </table> </div> With these extension methods, I read the configuration and if EnableBrowserShortCircuiting is true, I add the ShortCircuitMiddleware to my application.
### Understanding the Logging Configuration Data Configuration data for the logging is usually defined in the appsettings.json file.
### Creating Custom Log Messages The logging messages in the previous section were generated by the ASP.NET Core and MVC components that handled the HTTP request and generated the response. This kind of message can provide useful information, but you can also generate custom log messages that are specific to your application.
The AddMvcOptions method configures the most important MVC services. It accepts a function that receives an MvcOptions object, which provides one of the following set of configuration properties:
These configuration options are used to fine-tune the way your MVC application works. ## Conclusion In this post, I talked about configuring your ASP.NET Core MVC application. I showed how to enable logging, add built-in MVC functionalities and how to create a JSON file from which the application can read its configuration. If you want to learn more about more complex configurations, check my post Dealing with Complex Configurations in ASP.NET MVC Core. For more details about the configuring ASP.NET Core, I highly recommend the book “Pro ASP.NET Core MVC 2“. You can find the source code for this demo on GitHub.