2015 in review

The WordPress.com stats helper monkeys prepared a 2015 annual report for this blog.

Here's an excerpt:

A New York City subway train holds 1,200 people. This blog was viewed about 7,000 times in 2015. If it were a NYC subway train, it would take about 6 trips to carry that many people.

Click here to see the complete report.

Advertisements

Getting a Configuration Value in ASP.NET 5 (vNext) and MVC 6

As you might already know that things has been bit changed for reading appSettings in MVC 6 and Asp.Net 5.

We will go step by step how to achieve this.

First of all as we were used to add Application settings in Web.config till now. But now onwards we need to add these key value in file called config.json as below:


{
"AppSettings": {
    "MyKey" : "MyValue",
    "MyKey2" : "MyValue2"
}
}

 

After this we will create a class which will be used to deserialize those all configurations we provided above.

public class AppSettings
{
    public string MyKey { get; set; }
    public string MyKey2 { get; set; }
}

After this we need to make some change in Startup.cs file as below:


public class Startup
{
    // Set up application services
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMvc(routes =>
        {
             routes.DefaultHandler = new LocalizedRoute(routes.DefaultHandler);
             routes.MapRoute("default", "{controller}/{action}");
        });

        app.Configure(Configuration.GetSection("AppSettings"));
    }
}

 

Thats it!! Now you are able to use those application setting by simply injecting IOptions<AppSettings> in your controller as below:

public HomeController(IOptions<AppSettings> myAppSettings)
{
    var myKey = myAppSettings.Value.MyKey;
    var myKey2 = myAppSettings.Value.MyKey2;
}

Here IOptions<> interface is part of something called Options Model which is used for accessing POCO style settings(ex: your AppSettings) across your application.

Stay tuned for more updates!