Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 4 API's endpoint not responding

I am new to ASP.NET MVC4 and I have an issue I can't manage to resolve.

I have a simple GET endpoint which I want to return all the prestations.

When I try to reach this endpoint, I get no response and not even a timeout. My API is working well in other endpoints.

Here is the code inside the method related to this endpoint:

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

I simply can't see why this is not working.

Some help would be appreciated a lot.

Thanks !

like image 993
Pierrick Martellière Avatar asked Dec 03 '25 03:12

Pierrick Martellière


1 Answers

According to this article Handling Larger JSON String Values in .NET and Avoiding Exceptions you can try to increase the size of JSON string that you return by changing the web.config:

<configuration>  
   <system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="86753090" />
           </webServices>
       </scripting>
   </system.web.extensions>
</configuration> 

Also you can try to serialize the result with Newtonsoft.Json and return as a content to see if it works.

[ResponseType(typeof(List<Prestation>))]
public ActionResult GetPrestations()
{
    var presentations = db.Prestations.ToList();
    return Content(JsonConvert.Seriazlize(presentations), "application/json");
}

But in general you would't want to return data from entire table to the client because it can be huge and contain millions of records. So you would rather implement pagination:

[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();
}
like image 139
Andrii Litvinov Avatar answered Dec 05 '25 19:12

Andrii Litvinov