Questions tagged [jsonresult]

Represents a class that is used to send JSON-formatted content to the response.

252 questions
434
votes
32 answers

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

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)) || …
Kirk Ouimet
  • 23,368
  • 41
  • 108
  • 164
131
votes
15 answers

MaxJsonLength exception in ASP.NET MVC during JavaScriptSerializer

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…
Martin Buberl
  • 42,048
  • 25
  • 96
  • 139
103
votes
7 answers

Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible?

Is it possible to use JSON.NET as default JSON serializer in ASP.NET MVC 3? According to my research, it seems that the only way to accomplish this is to extend ActionResult as JsonResult in MVC3 is not virtual... I hoped that with ASP.NET MVC 3…
zam6ak
  • 6,999
  • 8
  • 42
  • 81
61
votes
1 answer

Can I convert a JSON string into JsonResult?

I have some stored JSON strings stored in the DB which I want to return to the client as JsonResult . I know that Json(object) turns an object into JsonResult but what if I already have the result in a string ? can I cast it to JsonResult
xantrus
  • 1,801
  • 1
  • 20
  • 41
46
votes
8 answers

How to unit test an Action method which returns JsonResult?

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 …
RPM1984
  • 69,608
  • 55
  • 212
  • 331
36
votes
5 answers

Actionresult vs JSONresult

I have 2 questions: What is the difference between JSONResult and ActionResult? When to use JSONResult in MVC?
28
votes
1 answer

JsonResult return Json in ASP.NET CORE 2.1

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…
blakcat
  • 526
  • 1
  • 4
  • 12
25
votes
5 answers

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

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 =…
ACP
  • 32,884
  • 96
  • 217
  • 360
14
votes
1 answer

ASP.NET MVC - Pass Json String to View using ViewData

I'm trying to pass Json to my View using ViewData Controller ViewData("JsonRegionList") = Json(RegionService.GetActiveRegions()) view $("input#UserRegion").autocomplete({ source:"<%: ViewData("JsonRegionList").ToString %>", …
Chase Florell
  • 42,985
  • 56
  • 169
  • 364
12
votes
1 answer

JsonResult or Json: which to use?

In ASP.NET MVC 3, which is more correct to use: Json() or new JsonResult()? Either returns the same result. Thanks for helping solve an office debate.
Darth Coder
  • 123
  • 1
  • 5
12
votes
5 answers

Returning JSON from a JsonResult method in MVC controller

I am trying to populate a ComboBox (Telerik RAD COmboBox) in a test ASP.NET MVC3 app. I have defined the ComboBox on my ASPX page and in the controller I have defined the action call that returns a JsonResult. The problem I am having is that the Web…
MAB
  • 123
  • 1
  • 1
  • 5
12
votes
2 answers

How to read a property of an anonymous type?

I have a method that returns return new System.Web.Mvc.JsonResult() { Data = new { Status = "OK", } } I need to write a unit test where I need to verify that jsonResult.Data.status= "OK". How do I read…
developer747
  • 13,032
  • 22
  • 78
  • 136
11
votes
1 answer

How should I return 404 from a JsonResult Controller?

In ASP.NET MVC5 I have a controller with a JsonResult return type. Depending on parameters I want to return a 404, as this is descriptive of the user requesting non-existent data. I could throw new HttpException(404, "message") but this feels dirty…
Matthew
  • 9,896
  • 5
  • 43
  • 95
11
votes
3 answers

calling @Html.Action for JsonResult changes my response type in parent template

I've got the following controller: public class HelloController { public ActionResult Index() { return View() } public ActionResult Hello() { return Json(new{ greeting = "hello, world!" },…
DMac the Destroyer
  • 4,770
  • 5
  • 29
  • 54
10
votes
3 answers

ASP.Net MVC: how to create a JsonResult based on raw Json Data

Having a string containing the following raw Json data (simplified for the sake of the question): var MyString = "{ 'val': 'apple' }"; How can I create a JsonResult object representing MyString? I tried to use the Json(object) method. but it…
SDReyes
  • 9,166
  • 15
  • 50
  • 91
1
2 3
16 17