我是 ASP.NET MVC4 的新手,我遇到了无法解决的问题。
我有一个简单的 GET 端点,我想返回所有 prestations
。
当我尝试到达此端点时,我没有收到任何响应,甚至没有超时。我的 API 在其他端点上运行良好。
这是与此端点相关的方法中的代码:
[Route("api/Prestations")]
// GET: api/Prestations
/// <summary>
/// Liste de toutes les prestations
/// </summary>
/// <returns></returns>
[ResponseType(typeof(List<Prestation>))]
public List<Prestation> GetPrestations()
{
return db.Prestations.ToList();
}
我就是不明白为什么这不起作用。
一些帮助将不胜感激。
谢谢!
最佳答案
根据这篇文章Handling Larger JSON String Values in .NET and Avoiding Exceptions您可以尝试通过更改 web.config 来增加返回的 JSON 字符串的大小:
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="86753090" />
</webServices>
</scripting>
</system.web.extensions>
</configuration>
您也可以尝试使用 Newtonsoft.Json 序列化结果并作为内容返回以查看它是否有效。
[ResponseType(typeof(List<Prestation>))]
public ActionResult GetPrestations()
{
var presentations = db.Prestations.ToList();
return Content(JsonConvert.Seriazlize(presentations), "application/json");
}
但通常您不会希望将整个表中的数据返回给客户端,因为它可能很大并且包含数百万条记录。所以你宁愿实现分页:
[ResponseType(typeof(List<Prestation>))]
public List<Prestation> GetPrestations(int skip = 0, int take = 100)
{
return db.Prestations.OrderBy(p => p.IdPrestation).Skip(skip).Take(take).ToList();
}
关于c# - ASP.NET MVC 4 API 的端点没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54830101/