Remark :
1 2 3 4 5 6 7 8 9 10 11 |
namespace JsonResultDemo.Models { public class UserModel { public int UserId { get; set; } public string UserName { get; set; } public string Company { get; set; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
using System; using System.Collections.Generic; using System.Text; using System.Web.Mvc; using System.Web.Script.Serialization; using JsonResultDemo.Models; namespace JsonResultDemo.Controllers { public class JsonDemoController : Controller { #region ActionControllers /// <summary> /// Welcome Note Message /// </summary> /// <returns>In a Json Format</returns> public JsonResult WelcomeNote() { bool isAdmin = false; //TODO: Check the user if it is admin or normal user, (true-Admin, false- Normal user) string output = isAdmin ? "Welcome to the Admin User" : "Welcome to the User"; return Json(output, JsonRequestBehavior.AllowGet); } /// <summary> /// Get tthe Users data in Json Format /// </summary> /// <returns></returns> public JsonResult GetUsersData() { var users = GetUsers(); return Json(users, JsonRequestBehavior.AllowGet); } /// <summary> /// Sample View /// </summary> /// <returns></returns> public ActionResult Sample() { return View(); } /// <summary> /// Update the user details /// </summary> /// <param name="usersJson">users list in Json Format</param> /// <returns></returns> [HttpPost] public JsonResult UpdateUsersDetail(string usersJson) { var js = new JavaScriptSerializer(); UserModel[] user = js.Deserialize<UserModel[]>(usersJson); //TODO: user now contains the details, you can do required operations return Json("User Details are updated"); } /// <summary> /// Get the huge list of Users /// </summary> /// <returns></returns> public JsonResult GetUsersHugeList() { var users = GetUsersHugeData(); return Json(users, JsonRequestBehavior.AllowGet); } #endregion #region Methods /// <summary> /// Get the Users /// </summary> /// <returns></returns> private List<UserModel> GetUsers() { var usersList = new List<UserModel> { new UserModel { UserId = 1, UserName = "Ram", Company = "Mindfire Solutions" }, new UserModel { UserId = 1, UserName = "chand", Company = "Mindfire Solutions" }, new UserModel { UserId = 1, UserName = "Abc", Company = "Abc Solutions" } }; return usersList; } /// <summary> /// Get the huge list of users /// </summary> /// <returns></returns> private List<UserModel> GetUsersHugeData() { var usersList = new List<UserModel>(); UserModel user; for (int i = 1; i < 51000; i++) { user = new UserModel { UserId = i, UserName = "User-"+i, Company = "Company-"+i }; usersList.Add(user); } return usersList; } /// <summary> /// Override the Json Result with Max integer json lenght /// </summary> /// <param name="data">Data</param> /// <param name="contentType">Content Type</param> /// <param name="contentEncoding">Content Encoding</param> /// <param name="behavior">Behavior</param> /// <returns>As JsonResult</returns> protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior) { return new JsonResult() { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior, MaxJsonLength = Int32.MaxValue }; } #endregion } } |
Test Tool
JsonResultDemo.Tests. JsonDemoControllerTest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using Microsoft.VisualStudio.TestTools.UnitTesting; using JsonResultDemo; using JsonResultDemo.Controllers; namespace JsonResultDemo.Tests.Controllers { [TestClass] public class HomeControllerTest { [TestMethod] public void Index() { // Arrange HomeController controller = new HomeController(); // Act ViewResult result = controller.Index() as ViewResult; // Assert Assert.IsNotNull(result); } [TestMethod] public void About() { // Arrange HomeController controller = new HomeController(); // Act ViewResult result = controller.About() as ViewResult; // Assert Assert.AreEqual("Your application description page.", result.ViewBag.Message); } [TestMethod] public void Contact() { // Arrange HomeController controller = new HomeController(); // Act ViewResult result = controller.Contact() as ViewResult; // Assert Assert.IsNotNull(result); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.Web.Mvc; using Microsoft.VisualStudio.TestTools.UnitTesting; using JsonResultDemo.Controllers; namespace JsonResultDemo.Tests.Controllers { [TestClass] public class JsonDemoControllerTest { [TestMethod] public void WelcomeNote() { JsonDemoController controller = new JsonDemoController(); JsonResult result = controller.WelcomeNote(); string msg = Convert.ToString(result.Data); // Assert Assert.AreEqual("Welcome to the User", msg); } } } |