6中未收到JsonResult的响应

6中未收到JsonResult的响应

本文介绍了在MVC 6中未收到JsonResult的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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:

在6 beta 4中使用Asp.Net MVC之前,同样的代码也可以正常工作.

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

有什么主意吗?

更新

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

推荐答案

您不需要显式返回Json.尝试直接返回对象.您可以根据需要使用[Produces]属性将输出限制为json,或者利用内容协商的格式可能更明智.

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

这篇关于在MVC 6中未收到JsonResult的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:15