Posts Tagged Read

How to read config settings in Asp.Net Core

Introduction

In this article I will explain the details of how to read config settings in Asp.Net Core.

Description

In Asp.Net webforms we used web.config file to store the config values like a connection string, appsettings etc.However, in Asp.Net core, the config file doesn’t exist anymore.  Instead, we have a slightly different approach where the configuration API provides a way of configuring an app based on a list of name-value pairs that can be read at runtime from multiple sources. You can read the config values from multiple sources like JSON, XML etc.

In this article we will examine how to implement config files based on JSON formats in Asp.Net Core application.

Add the Required references

To read the values from appsettings.json, we need to add the below references to project

Add appsettings.json file to store config values

We will store the config items as Name:Value pair in this json file.  You can also add hierarchical list in json file where each items will be separated by comma.

Sample value

Create ConfigurationBuilder in Startup.cs class

We use AddJsonFile” method in JsonConfigurationExtensions class to provide the Json File path. To reload the configuration file if the file changes we can set the reloadOnChange property.  You can find details of AddJsonFile method here.

Read the value from appsetttings.json file

Using the GetSection method we can access the values from appsettings.json file

Complete Code

You can find the complete source code for this article here

Advertisements

, , ,

Leave a comment

How to read JSON with object name using Json.Net in C#

Introduction:
In this article I am going to explain how to read the values from Json using Json.Net library when it is having Object Name.

Description:

In this article we are using Json.Net library to parse and read values in Json. We will be using JSON serializer methods available in this dictionary.

If you don’t have Json Framework in your machine you can get it from Nugget for Json.

Sample JSon file which we use in this article

{"employees":[
{"firstName":"Test1FN", "lastName":"Test1LN"},
{"firstName":"Test2FN", "lastName":"Test2LN"}
]}

Implementation:

After adding library to your project ensure that you have added the namespace reference in your page

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Next add a class to hold the values, note that I have used JsonProperty to map the JSON property to a .NET member or constructor parameter. This is very important as the object name format is not following key: value combination so if you don’t set this then while reading you will get exception.

[JsonObject(MemberSerialization.OptIn)]
public class User
{
   [JsonProperty("firstName", Required = Required.Always)]
   public string FirstName { get; set; }

   [JsonProperty("lastName", Required = Required.Always)]
   public string LastName { get; set; }
}

Now you can use the below code to read the values

//Load your Json string here
String jsonString = "{\"user\":[{\"firstName\": \"Test1FN\", \"lastName\": \"Doe\"},{\"firstName\": \"Test2FN\", \"lastName\": \"Test2LN\"}]}";
JToken root = JObject.Parse(jsonString);
JToken user = root["user"];
//Deserialize the Json to list
var deserializedUser = JsonConvert.DeserializeObject<List<User>>(user.ToString());

//Loop through values 
foreach (User p in deserializedUser)
{
//Your code to process the values
}

, , , ,

Leave a comment