gpt4 book ai didi

c# - MVC 6 中的 JsonResult 未收到任何响应

转载 作者:太空狗 更新时间:2023-10-29 23:50:26 34 4
gpt4 key购买 nike

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

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

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

在我的 Controller 中我有:

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

但是当我运行 postman 时,我收到以下消息: enter image description here

之前我在 6 beta 4 中使用 Asp.Net MVC 并且同样的代码有效。

知道哪里出了问题吗?

更新

我的路线:

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> myClass = await _Repository.GetListMyClass();

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

它不会转换为 JSON ......对吧?

关于c# - MVC 6 中的 JsonResult 未收到任何响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31845430/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com