Ben Sin
Ben Sin

Reputation: 41

C# ApiController returns List but postman shows "Could not get any response"

I am struggling with strange situation. I am calling void method to ApiController from postMan but I get strange results. ApiController captures request and returns result with 3 objects but postman somehow shows that could not get any respone.. While all other methods which return one value works fine problem accours then returning multiple objects.

ApiController:

[HttpGet]
[Route("api/documents/AllDocs/")]
public List<Document> AllDocs()
{
    lock (_lock)
    {
        _documentsRepository = DocumentsRepository.Instance;
        var result = _documentsRepository.GetDocuments();

        return result;
    }
}

Documents repository:

public List<Document> GetDocuments()
{
    var documents = new List<Document>();
    try
    {
        var db = new Context();
        var docs = db.Document;
            //.Include(x => x.DocumentItems)
            //.Include(x => x.Page.Select(y => y.RegionCropper.Select(z => z.Cropper)))
            //.Include(x => x.Page.Select(y => y.RegionCropper.Select(z => z.MinorCropper))).ToList();
        return docs.ToList();
    }
    catch (Exception ex)
    {
        return documents;
    }
}

PostMan :

enter image description here

WebConfig :

public static class WebApiConfig
{
    private static HttpSelfHostServer _server;

    public static void Run(string port)
    {
        var config = new HttpSelfHostConfiguration($"http://localhost:{port}");//"http://localhost:8080");

        config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

        config.Routes.MapHttpRoute(
        name: "API Default",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional });

        //config.Routes.MapHttpRoute(
        //    "newdocument", "api/documents/newdocument/{document}",new { document = RouteParameter.Optional });


        config.MaxReceivedMessageSize = int.MaxValue;;
        config.MaxBufferSize = int.MaxValue;
        _server = new HttpSelfHostServer(config);
        _server.OpenAsync();

    }

    public static void Stop()
    {
        _server?.CloseAsync();
    }
}

Upvotes: 1

Views: 2386

Answers (1)

Felix Gerber
Felix Gerber

Reputation: 1651

It's a shot in the dark, but I lately faced the same problem.

I had an issue with my JSON-Config.

In my model I had a class User with a List of Postings:

public class User
{
    public long Id { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public List<Posting> Postings{ get; set; }
}

The model of those Postings looked like that:

public class Posting
{
    public long Id { get; set; }
    public double Value { get; set; }
    public string Name { get; set; }
    public DateTime PostingDate { get; set; }
    public virtual User User { get; set; }
}

Whenever I returned a List<Posting> Postman said could not get any response.

The problem here was the Json-serialization. When trying to serialize my Model, Json faced an self refferencing loop, but didn't threw an exception.

The only thing I had to do to get rid of this is adding [] to the User in my Posting Model:

[JsonIgnore] 
public virtual User User { get; set; }

I relazied that would to the trick when I tried to serialize the List programatically, because that way the exception was thrown.

Upvotes: 4

Related Questions