Tag: jsonresult

如何unit testing返回JsonResult的Action方法?

如果我有这样的控制器: [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); } 基本上,我从我的仓库中获取东西,然后将其投影到匿名types的List<T>中。 我如何进行unit testing? System.Web.Mvc.JsonResult有一个名为Data的属性,但是它的types是object ,正如我们所期望的那样。 那么这是否意味着如果我想testingJSON对象具有我期望的属性(“id”,“name”,“type”),我必须使用reflection? 编辑: 这是我的testing: // Arrange. const string autoCompleteQuery = "soho"; // Act. var actionResult = _controller.FindLocations(autoCompleteQuery); // Assert. Assert.IsNotNull(actionResult, […]

JavaScriptSerializer期间ASP.NET MVC中的MaxJsonLengthexception

在我的一个控制器动作中,我返回一个非常大的JsonResult来填充网格。 我收到以下InvalidOperationExceptionexception: 在使用JSON JavaScriptSerializer进行序列化或反序列化时出错。 string的长度超过maxJsonLength属性中设置的值。 不幸的是,将web.config的maxJsonLength属性设置为更高的值并不会显示任何效果。 <system.web.extensions> <scripting> <webServices> <jsonSerialization maxJsonLength="2147483644"/> </webServices> </scripting> </system.web.extensions> 我不想把它作为一个string,就像在这个答案中提到的那样。 在我的研究中,我遇到了这个博客文章,写一个自己的ActionResult (例如LargeJsonResult : JsonResult )被推荐来绕过这个行为。 这是唯一的解决scheme吗? 这是ASP.NET MVC中的错误? 我错过了什么吗? 任何帮助将不胜感激。

使用JSON.NET作为ASP.NET MVC 3中的默认JSON序列化程序 – 有可能吗?

是否有可能在ASP.NET MVC 3中使用JSON.NET作为默认的JSON序列化程序? 根据我的研究,似乎要做到这一点的唯一方法是将ActionResult扩展为MVC3中的JsonResult不是虚拟的 … 我希望在ASP.NET MVC 3中有一种方法可以指定一个可序列化为JSON的可插件提供者。 思考?