About JsonResult and its properties

The JSON format is an open standard format. The format of data looks very easy to understand and the data objects consist of attribute-value pairs.

  • ContentEncoding: It helps to indicate the content encoding type, the default encoding for JSON is UTF-8.
  • ContentType: It helps to indicate the content type. The default content type for JSON is application/json; charset=utf-8.
  • Note: ContentType and ContentEncoding are not necessary to mention when sending the data in JSON format as the HTTP headers are having a responsibility to tell the recipient what kind of content they’re dealing with.
  • Data: This indicates what the content data is, that means what you will send in JSON format.
  • JsonRequestBehavior: This property has two options. Those are AllowGet and DenyGet. The default option is DenyGet. When you send data in JSON format, using Get Request, it’s necessary to specify the property as AllowGet otherwise it shows the error as “The request would be blocked since the JSON data is considered as sensitive data information”.
  • MaxJsonLength: This helps to get or set the maximum JSON content length that you will send. The default value for this is 2097152 characters, that is equal to 4 MB of Unicode string data. You can even increase the size based if needed, for that you will get an idea later in this article.
    RecursionLimit: Indicates the constraining number of object levels to process. The default value is 100. It means you can serialize the objects that are nested to a depth of 100 objects referencing each other. In a general scenario, the default limit 100 is obviously sufficient when you deal with a JsonResult so there is no need to increase it even though you have the option to increase the limit if required.

Example ;

Update the code in UserMode.cs with the following code.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace JsonResultDemo.Models  
  7. {  
  8.     public class UserModel  
  9.     {  
  10.         public int UserId { getset; }  
  11.         public string UserName { getset; }  
  12.         public string Company { getset; }  
  13.     }  

Add one method named GetUsers in the JsonDemoController.cs file that will return the list of sample users.

  1.   /// <summary>  
  2.         /// Get the Users  
  3.         /// </summary>  
  4.         /// <returns></returns>  
  5.         private List<UserModel> GetUsers()  
  6.         {  
  7.             var usersList = new List<UserModel>  
  8.             {  
  9.                 new UserModel  
  10.                 {  
  11.                     UserId = 1,  
  12.                     UserName = “Ram”,  
  13.                     Company = “Mindfire Solutions”  
  14.                 },  
  15.                 new UserModel  
  16.                 {  
  17.                     UserId = 1,  
  18.                     UserName = “chand”,  
  19.                     Company = “Mindfire Solutions”  
  20.                 },  
  21.                 new UserModel  
  22.                 {  
  23.                     UserId = 1,  
  24.                     UserName = “Abc”,  
  25.                     Company = “Abc Solutions”  
  26.                 }  
  27.             };  
  28.   
  29.             return usersList;  
  30.         } 

Create one Action Controller method named GetUsersData with the following code in the JsonDemoController.cs file. 

 

  1. /// <summary>  
  2.         /// Get tthe Users data in Json Format  
  3.         /// </summary>  
  4.         /// <returns></returns>  
  5.         public JsonResult GetUsersData()  
  6.         {  
  7.             var users = GetUsers();  
  8.             return Json(users, JsonRequestBehavior.AllowGet);  
  9.         }