在MVC 6中没有收到JsonResult的回复

我在beta6中使用ASP.NET MVC 6。

在我的Startup.cs中,我有以下代码:

services.AddMvc().Configure(o => { o.OutputFormatters.RemoveAll(formatter => formatter.GetType() == typeof(JsonOutputFormatter)); var jsonOutputFormatter = new JsonOutputFormatter { SerializerSettings = { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore } }; o.OutputFormatters.Insert(0, jsonOutputFormatter); }); 

在我的控制器中,我有:

 public async Task Get() { var result = new DataTablesResult(); List myClass = await _Repository.GetListMyClass(); result.Data = new List(myClass); var resultado = new { data = result.Data.ToArray() }; return Json(resultado); } 

但是当我运行邮差时,我收到以下消息: 在此处输入图像描述

之前我在6 beta 4中使用Asp.Net MVC并且相同的代码工作。

知道什么可能是错的吗?

UPDATE

我的路线:

 app.UseMvc(routes => { // add the new route here. routes.MapRoute(name: "config", template: "{area:exists}/{controller}/{action}", defaults: new { controller = "Home", action = "Index" }); routes.MapRoute( name: "configId", template: "{area:exists}/{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index" }); routes.MapRoute(name: "platform", template: "{area:exists}/{controller}/{action}", defaults: new { controller = "Home", action = "Index" }); routes.MapRoute(name: "platformByUser", template: "{area:exists}/{controller}/{action}/{userId}"); routes.MapRoute( name: "default", template: "{controller}/{action}/{id?}", defaults: new { controller = "Home", action = "Index" }); // Uncomment the following line to add a route for porting Web API 2 controllers. // routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}"); }); 

我怀疑下面的代码,DataTable /动态到JSON ???

 **var result = new DataTablesResult();** List myClass = await _Repository.GetListMyClass(); **result.Data = new List(myClass);** var resultado = new { data = result.Data.ToArray() }; 

它不会转换为JSON ……对吗?

我注意到你将属性ReferenceLoopHandling设置为Newtonsoft.Json.ReferenceLoopHandling.Ignore 。 据我所知, JSON输出格式化程序和JsonResult的设置现在是分开的 。 如果MyClass有循环,则Json.net中会发生错误。

检测到自引用循环

在这种情况下有两种解决方案:

1)使用控制器方法的自定义序列化设置:

 var settings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }; return Json(result, settings); 

2)在JchoResult和Startup中的格式化程序之间配置共享设置:

 public void ConfigureServices(IServiceCollection services) { //some configuration services.AddMvc() .AddJsonOptions(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; }); } 

我希望这能帮到您

您不需要显式返回Json。 尝试直接返回您的对象。 如果您愿意,可以使用[Produces]属性将输出限制为json,或者获得格式内容协商的优势可能更明智。

 public async Task Get() { var result = new DataTablesResult(); List myClass = await _Repository.GetListMyClass(); result.Data = new List(myClass); var resultado = new { data = result.Data.ToArray() }; return Ok(resultado); }