Response from JsonResult in MVC 6 not received

I am using ASP.NET MVC 6 in beta.

In my Startup.cs, I have the following code:

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

In my controller, I:

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

But when I start the postman, I get the following message: enter image description here

Before I used Asp.Net MVC in 6 beta 4 and the same code worked.

Any idea what could be wrong?

<strong>

UPDATE

My routes:

 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?}"); }); 
+5
source share
3 answers

I suspect the below code is DataTable / dynamic for JSON ???

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

It is not hidden from JSON ... right?

0
source

I noticed that you set the ReferenceLoopHandling property in Newtonsoft.Json.ReferenceLoopHandling.Ignore. As I know, the settings for formatting JSON output and for JsonResult are now highlighted . If MyClass has loops, an error occurs in Json.net.

Self-regulation cycle detected

In this case, there are two solutions:

1) Use custom serialization options for the controller method:

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

2) Configure general settings between JsonResult and formatting in Startup:

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

I hope this helps you

0
source

You do not need to explicitly return Json. Try to directly return your object. You can limit the json output to the [Produces] attribute if you want, or take advantage of content negotiation for the format, maybe wiser.

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

All Articles