No response received with JsonResult in MVC 6

早过忘川 提交于 2019-12-07 05:47:56

问题


I'm using ASP.NET MVC 6 in beta6.

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 have:

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 run the postman, I get the following message:

Before I was using Asp.Net MVC in 6 beta 4 and this same code worked.

Any idea what could be wrong?

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?}");
            });

回答1:


I suspect the below code, DataTable / dynamic to 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 doesn't covert to JSON ... right ?




回答2:


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

Self referencing loop detected

There are two solutions in this case:

1) Use a custom serialize settings for the controller method:

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

2) Configung a shared settings between JsonResult and the formatter in the Startup:

public void ConfigureServices(IServiceCollection services)
{
    //some configuration

    services.AddMvc()
        .AddJsonOptions(options =>
        {
            options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        });
}

I hope this will help you




回答3:


You don't need to return a Json explicitly. Try returning your object directly. You can limit the output to json with the [Produces] attribute if you like, or get advantage of content negotiation for format may be 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);
}


来源:https://stackoverflow.com/questions/31845430/no-response-received-with-jsonresult-in-mvc-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!