How to read App Settings values from Config.json or appsettings.json file in ASP.NET Core

In this article we are going to learn how can we get/read values from config.json or appsettings.json file in Asp.Net core.

Assume you have following settings in your config file

{
  "MySettings": {
        "key": "12345"
    }
}


Now, in order to read values from above config file, you need the Configuration object which is available in the namespace 'Microsoft.Extensions.Configuration'. 

Open your startup.cs file and add reference to 'Microsoft.Extensions.Configuration' namespace. You need to use dependency injection to get Configuration object in either your controller or any other class file. Dependency Injection is a built-in feature in .NET core.

To achieve this, add the following line inside ConfigureServices method in startup.cs file.

services.AddSingleton<IConfiguration>(Configuration);

Now, your new ConfigureServices method will be

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddSingleton < IConfiguration > (Configuration);
}

And, your startup.cs file should look like this



As we added dependency injection for IConfiguration, we can start using it in any controller or any other class.

Reading values from appsettings.json file:


We have to use the constructor injection in any controller for reading values from appsettings.json file. Just add a constructor in your controller which takes IConfiguration interface as input parameter. 
At runtime, when we the controller gets called, this Constructor [MyController(IConfiguration configuration)] will get called and it will get resolved by its concrete class [Configuration] which we have set in Startup class.

 public class MyController : Controller
    {
        private readonly IConfiguration Configuration;

        public MyController(IConfiguration config)
        {
            this.Configuration = config;
        }
}

Now as we have the reference to Configuration object in our controller we can now read the values from appsettings.json file using following code

            var value = this.config.GetSection("MySettings").GetSection("key").Value;

This is how we set or get values from appsettings.json file in  .Net core projects.

For more useful articles on .Net core visit: Asp.Net core


Read more...

How to remove old and unused Docker images or stop Docker Containers or remove Docker Volumes

This post explains how to remove old or unused docker images, how to remove stopped containers, how to remove unused volumes, how to remove unused networks or a single command to run all prunes at a time

Note: These command works for Docker with version 1.13 or higher

Command to remove unused Images

docker image prune

Command to removed stopped Containers


docker container prune

Command to remove unused Volumes


docker volume prune

Command to remove unused Networks


docker network prune

Command to run all Prunes at a time


docker system prune

It is suggested that not to use system prune command. This may remove the things which users doesn't want to remove.

This is how we remove old or unused docker images, or remove stopped containers, or remove unused volumes, or remove unused networks or run all prunes at a time.

For more posts on Docker please visit: docker

Read more...

angular ng grid not displaying data when there is a special character in column name

In this post am going to explain how to show data in ng-grid when there is a special character in column name.

Assume you have following columnDefs in gridOptions

columnDefs: [{
      name: "parent.name",
      displayName: 'Name',
      type:'string'

    }, {
      name: 'lastName',
      displayName: 'Surname'
    }]
  };

As you see, our first column has name as parent.name. It has dot ('.') in it's name. So if you use this columnDefs in your ng-grid it will not show data in that column as the name has special characters in it. To make it work you need to add following flag to your gridOptions

gridOptions.flatEntityAccess = true;

So your final gridOptions will look something like below

  $scope.gridOptions = {

    columnDefs: [{
      name: "parent.name",
      displayName: 'Name',
      type:'string'

    }, {
      name: 'lastName',
      displayName: 'Surname'
    }]
  };

  $scope.gridOptions.data = [{
    "parent.name": "John",
    "lastName": "Cena"
  }];

 $scope.gridOptions.flatEntityAccess = true;

Check the following plunkr for working example.

ng-grid show data when there is a special character in column name

This is how we can show data in ng-grid even if the column name is having special character in it.

For more posts on angularJs refer: AgngularJs

Read more...

how to merge a specific commit in Git

In this article am going to explain how to merge(cherry pick) specific commit in Git

When you are working on two different branches you often merge code from one branch to another. But if you want to merge specific changes to another branch you can do it by cherry picking specific commit.

Using Git Extensions you can easily merge a specific commit. (Download Git Extensions from link1 or from here link2)

In Git Extensions open your repository. It will show you all the commits. Select any specific commit and right click on it. Then select cherry pick commit. Check the below screenshot for reference


Once you select Cherry pick commit it opens another small popup as shown below


Check "Automatically create a commit" if you want to automatically commit this changes into current branch or if you want to manually commit, keep the box unchecked and click on "Cherry pick".

In this way you can merge a specific commit in Git
Read more...

ng-options with simple and complex array angularjs

In this article am going to explain how to use ng-options with simple and complex(array of objects) arrays

Assume you have following array

$scope.fruits = ['apple', 'banana', 'orange'];

If you want to show them in a dropdown using ng-options use the following code

<select name="fruit" data-ng-model="selectedFruit"  data-ng-options="fruit as fruit for fruit in fruits"></select>

What you get in the resulting html is below

<option label="apple" value="string:apple">apple</option>
<option label="banana" value="string:banana">banana</option>
<option label="orange" value="string:orange">orange</option>

when you select any option the value gets selected is same as value shown in dropdown.

Use ng-options for array of objects:


If you have an array of objects like below

$scope.fruits = [{ id: 1, name: 'apple' }, { id: 2, name: 'banana' }, { id: 3, name: 'orange' }];

Use the following html to show them in dropdown

<select name="fruit" data-ng-model="selectedFruit"  data-ng-options="fruit.id as fruit.name for fruit in fruits"></select>

If you use the above html then in the dropdown it shows name of the fruit but internally it stores the id of the fruit when you select any value from dropdown. resulting html code looks like below

<option label="apple" value="number:1">apple</option>
<option label="banana" value="number:2">banana</option>
<option label="orange" value="number:3">orange</option>

When you click on dropdown it shows names of the fruits but when you select any option internally id of the object will be selected.

Check this Plunker for working example: ng-options with simple and complex arrays

This is how ng-options works with simple and complex arrays.

For more posts on angularjs visit: AngularJS

Read more...