There are two ways to return JSON-formatted content to caller:
1. MVC framework provides the JsonResult class to send JSON-formatted content to the caller. Suppose you have a C# object and you would like to return this C# object as JSON string to caller, you can use Json() method.
[HttpPost]
public JsonResult ValidateUser(string Password)
{
if (Password == ConfigurationManager.AppSettings["AccessPassword"])
return Json(new { Valid = true });
else
return Json(new { Valid = false });
}
2. If you have a valid JSON string at the backend and you would like to return it to front end, you can use ContentResult class to send JSON-formatted content to the caller.
public ContentResult CalculateResults()
{
return new ContentResult { Content = @" { ""Year2014"": 100, ""Year2015"": 200, ""Year2016"": 300 }", ContentType = "application/json" };
}
1. MVC framework provides the JsonResult class to send JSON-formatted content to the caller. Suppose you have a C# object and you would like to return this C# object as JSON string to caller, you can use Json() method.
[HttpPost]
public JsonResult ValidateUser(string Password)
{
if (Password == ConfigurationManager.AppSettings["AccessPassword"])
return Json(new { Valid = true });
else
return Json(new { Valid = false });
}
2. If you have a valid JSON string at the backend and you would like to return it to front end, you can use ContentResult class to send JSON-formatted content to the caller.
public ContentResult CalculateResults()
{
return new ContentResult { Content = @" { ""Year2014"": 100, ""Year2015"": 200, ""Year2016"": 300 }", ContentType = "application/json" };
}
Comments