jsonresult

Actionresult vs JSONresult

南笙酒味 提交于 2019-11-28 20:09:16
I have 2 questions: What is the difference between JSONResult and ActionResult? When to use JSONResult in MVC? ActionResult is an abstract class that an action can return. The helper methods in Controller (eg, Json() , Content() , View() , ...) return different concrete classes that inherit ActionResult , including JsonResult . You should declare your action methods as returning ActionResult , so that they have the freedom to return any concrete result class. Use JsonResult when you want to return raw JSON data to be consumed by a client (javascript on a web page or a mobile client). Use

How to unit test an Action method which returns JsonResult?

佐手、 提交于 2019-11-28 18:10:26
If I have a controller like this: [HttpPost] public JsonResult FindStuff(string query) { var results = _repo.GetStuff(query); var jsonResult = results.Select(x => new { id = x.Id, name = x.Foo, type = x.Bar }).ToList(); return Json(jsonResult); } Basically, I grab stuff from my repository, then project it into a List<T> of anonymous types. How can I unit-test it? System.Web.Mvc.JsonResult has a property called Data , but it's of type object , as we expected. So does that mean if I want to test that the JSON object has the properties I expect ("id", "name", "type"), I have to use reflection?

How to redirect to a controller action from a JSONResult method in ASP.NET MVC?

≡放荡痞女 提交于 2019-11-28 17:30:51
问题 I am fetching records for a user based on his UserId as a JsonResult... public JsonResult GetClients(int currentPage, int pageSize) { if (Session["UserId"] != "") { var clients = clirep.FindAllClients().AsQueryable(); var count = clients.Count(); var results = new PagedList<ClientBO>(clients, currentPage - 1, pageSize); var genericResult = new { Count = count, Results = results }; return Json(genericResult); } else { //return RedirectToAction("Index","Home"); } } How to redirect to a

Sorting not working with Json Result giving encoded output

∥☆過路亽.° 提交于 2019-11-28 11:15:27
问题 I am using Json Result to show a table, it is working fine when I show the result. Now I wanted to add a sort feature to it, so I used the canSort:true property. But now whenver I click on the header of the table for the sort to happen I get the below encoded string in my browser, it seems it is sorted too but some kind of encoding is done to it, it is as below. {"Data":"\u003ctable class=\"paramCustomDataTable\"\u003e\u003cthead\u003e\u003ctr class=\"customHead\"\u003e\u003cth scope=\"col\"

JsonResult return Json in ASP.NET CORE 2.1

烈酒焚心 提交于 2019-11-28 02:30:17
问题 Controller that worked in ASP.NET Core 2.0: [Produces("application/json")] [Route("api/[controller]")] [ApiController] public class GraficResourcesApiController : ControllerBase { private readonly ApplicationDbContext _context; public GraficResourcesApiController(ApplicationDbContext context) { _context = context; } [HttpGet] public JsonResult GetGrafic(int ResourceId) { var sheduling = new List<Sheduling>(); var events = from e in _context.Grafic.Where(c=>c.ResourceId == ResourceId) select

Pass JSON Object To MVC Controller as an Argument

走远了吗. 提交于 2019-11-27 15:19:06
I have the following arbitrary JSON object(The field names may be changed). { firstname: "Ted", lastname: "Smith", age: 34, married : true } - public JsonResult GetData(??????????){ . . . } I know that I can define a class just like the JSON object with the same field names as the argument, But I would like my controller to accept arbitrary JSON object with different field names. If you want to pass custom JSON object to MVC action then you can use this solution, it works like a charm. public string GetData() { // InputStream contains the JSON object you've sent String jsonString = new

Actionresult vs JSONresult

谁说胖子不能爱 提交于 2019-11-27 12:46:18
问题 I have 2 questions: What is the difference between JSONResult and ActionResult? When to use JSONResult in MVC? 回答1: ActionResult is an abstract class that an action can return. The helper methods in Controller (eg, Json() , Content() , View() , ...) return different concrete classes that inherit ActionResult , including JsonResult . You should declare your action methods as returning ActionResult , so that they have the freedom to return any concrete result class. 回答2: Use JsonResult when you

Pass JSON Object To MVC Controller as an Argument

落花浮王杯 提交于 2019-11-26 17:07:07
问题 I have the following arbitrary JSON object(The field names may be changed). { firstname: "Ted", lastname: "Smith", age: 34, married : true } - public JsonResult GetData(??????????){ . . . } I know that I can define a class just like the JSON object with the same field names as the argument, But I would like my controller to accept arbitrary JSON object with different field names. 回答1: If you want to pass custom JSON object to MVC action then you can use this solution, it works like a charm.

Fastest way to check if a string is JSON in PHP?

不羁的心 提交于 2019-11-26 09:26:26
I need a really, really fast method of checking if a string is JSON or not. I feel like this is not the best way: function isJson($string) { return ((is_string($string) && (is_object(json_decode($string)) || is_array(json_decode($string))))) ? true : false; } Any performance enthusiasts out there want to improve this method? function isJson($string) { json_decode($string); return (json_last_error() == JSON_ERROR_NONE); } Answer to the Question The function json_last_error returns the last error occurred during the JSON encoding and decoding. So the fastest way to check the valid JSON is //

MaxJsonLength exception in ASP.NET MVC during JavaScriptSerializer

浪尽此生 提交于 2019-11-26 03:35:39
问题 In one of my controller actions I am returning a very large JsonResult to fill a grid. I am getting the following InvalidOperationException exception: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property. Setting the maxJsonLength property in the web.config to a higher value unfortunately does not show any effect. <system.web.extensions> <scripting> <webServices> <jsonSerialization