Lesson learnt Configuration load issue in ASP.NET MVC core (Mumbai).

This is was an interesting problem which we came across in one of our recent ASP.NET MVC Mumbai batches in Andheri.
In ASP.NET MVC core all settings are stored in Appsettings.Json file. So one of our students had saved the connection string in the JSON files as shown in the below code.

{
"ConnectionString" :  "SomeServer"
}

He was reading this configuration data in a class as shown in the below code. This class was dependency injected in all the controllers during the HTTP request.

publicclassMyConfiguration
{
publicstring ConnectionString = “”;
}

The mapping between the class and the JSON file was done in the configureservices class which is in startup.cs.

publicvoid ConfigureServices(IServiceCollection services)
{
services.Configure(Configuration);

}

When the controller was requested the WebAPI was not getting the connection string details. It was coming empty fresh object. After hunting for 15 minutes we found out that the class can not simple variables it should be a SET GET property.

publicclassMyConfiguration
{
publicstring ConnectionString { get; set; }
}

Do not miss our next ASP.NET MVC batch coming week in Mumbai
http://stepbystepschools.net/?page_id=315

Comments

comments

This entry was posted in ASP.NET MVC Training, Class Room Training, MVC, MVC Training in Mumbai and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.