The New Configuration Model in ASP.NET Core

Wednesday, April 13, 2016 Posted by mahdi tahanian under .Net Core Core has a lot of changes compared with the others versions of ASP.NET.

One change I want to highlight is the new way to configure settings. With previous versions, this configuration was made by using the common file web.config, but this file simply doesn’t exist anymore in ASP.NET Core.
Instead, a new easy way to declare and access to the settings is provided. Let’s take a look.

Goodbye web.config

Before we examine the new way to configure settings, let’s look at how it was done in versions prior to ASP.NET Core.

First, you go to the web.config file and, inside the appsettings node, add all the settings that you need. This works like a key-value dictionary:

    <add key="SmtpServer" value="" />
    <add key="SmtpUser" value="" />
    <add key="SmtpPass" value="12345678" />
    <add key="SmtpPort" value="25" />
    <!-- More Settings -->

The next step is to access these values. This is done using theConfigurationManager class:


Hello appsettings.json

In ASP.NET Core, it is possible to read settings from different sources like XML, JSON and INI files. To see how it works in your ASP.NET Core Application, add a new section to store the SMTP info in theappsettings.json file:

  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Verbose",
      "System": "Information",
      "Microsoft": "Information"
  "Smtp": {
    "Server": "",
    "User": "",
    "Pass": "123456789",
    "Port": "25"

One of the key advantages in this new configuration model is that it’s easy to create a class to work with the data. To do that for the example above, create a new class called SmtpConfig inside the folder ViewModels (or whatever folder you prefer) and define four properties:

public class SmtpConfig

    public string Server { get; set; }
    public string User { get; set; }
    public string Pass { get; set; }
    public int Port { get; set; }

Next, go to the Startup.cs class, add the following code within the constructor to read the appsettings.json file:

var builder = new ConfigurationBuilder()
Configuration = builder.Build();

In the same class, go to the ConfigureServices function. The following code will read the Smtp section, then use the Configuremethod of Services to set this to the SmtpConfig class:

public void ConfigureServices(IServiceCollection services)
    //Other services

Using Configuration

ASP.NET Core has built-in dependency injection. To use the SmtpConfigclass, you can inject them using the Interface IOptions<T>. The following examples usesthis in a MVC Controller:

public class HomeController : Controller
    public SmtpConfig SmtpConfig { get; }
    public HomeController(IOptions<SmtpConfig> smtpConfig)
        SmtpConfig = smtpConfig.Value;
    //Action Controller

Looking at the constructor, you’ll see that it’s injecting an object of typeSmtpConfig through IOptions<SmtpConfig> and assigning it to a property of the same type:

All’s good and pretty easy.

Configuring Multiple Environments

What if you need different configuration values for different environments? If you remember web.config transformations, there is a similar concept here that is related to determining the environment (dev, QA, staging, production, etc). Let’s see how it works.

Add a new JSON file and set the name then add only the smtp section

with the values associated with this new environment:

    "Smtp": {
        "Server": "",
        "User": "",
        "Pass": "Ke!2546hg#$",
        "Port": "427"

So how does the framework know what the current environment is? There is an environment variable called Hosting:Environment. To access it inside Visual Studio, open the project properties, go to the Debug tab and change the value for this variable to qa:


Return to Startup.cs class and next to the line that read the appsettings file, tell the framework to read the additional appsettings file base on the environment. If this file exists, the property values are overridden:

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

Test the application again, and look the values that are used this time:



APS.NET Core offers a new configuration model that is clean and very easy to use. Plus it works with XML, it works with JSON, INI and any other file type you may need. Lastly, it is very customizable and is built with dependency injection in mind.

Tagged .Net Core  New Configuration Model  ASP.NET Core  1 Comments

jQuery 1.12.2 and 2.2.2 Released

Friday, March 18, 2016 Posted by mahdi tahanian under JQuery 

GitHub changelog


Tagged Jquery  jquery 2.2.2  0 Comments

Better Ways to Build a Database

Wednesday, March 16, 2016 Posted by mahdi tahanian under SQL Server 

The purpose of a database build is simple: prove that what you have in version control can successfully create a working database, and yet many teams struggle with unreliable and untested database build processes that slow down deployments and prevent the delivery of new functionality. Grant Fritchey explains how to achieve an automated and reliable database build that is only as complex as the database system it needs to create.


Tagged better-ways-to-build-a-database  build-a-database  0 Comments

SharePoint 2016

Wednesday, March 16, 2016 Posted by mahdi tahanian under Server  News 

Today marks Release to Manufacturing (RTM) of SharePoint Server 2016! This is an important milestone in the delivery of this significant release, which includes new capabilities for users, IT pros and administrators, as well as the next generation of hybrid capabilities for SharePoint. This also marks the general availability of cloud hybrid search for SharePoint Server 2013 and higher customers—allowing on-premises and Office 365 content to be surfaced in one search result.


Tagged SharePoint 2016  0 Comments

Support No Sql In Entity Framework 7

Wednesday, March 16, 2016 Posted by mahdi tahanian under MVC 

  • Support for non-relational data stores and in-memory data
  • Support for Windows Phone and Windows Store applications
  • Support for Linux and Macintosh machines
See the Refrence 

Tagged Entity framework  Entity Framework 7  0 Comments