UPDATE: for people having the same error/behaviour see answer stackoverflow.com/a/42813181/1155847 - my own stupid mistake, but the error message - well no real error message was given actually.. So let's say, the behaviour was quite strange.. At least, it's solved now.
Original question/problem:
I'm learning to build a REST-ful service with WebAPI, you can download the code at: https://github.com/pluralsight-courses/Implementing-and-Securing-an-API-with-ASP.NET-Core/tree/master/Module02_ReadingData *~ see note below when cloning.
I'm having trouble with the return of a parameter when I call the GET of code below with the querystring localhost:8088/api/camps/1?includeSpeakers=true
- both postman and browser give me back "Server Not Found", and the browser even redirects to http://www.localhost.com:8088/api/camps/1?includeSpeakers=true
... Strange behaviour for me as the other requests, such as localhost:8088/api/camps/1
and localhost:8088/api/camps
just work fine.
If I debug and step through I see it's going through fine and picking up the QueryString, executing it against the database, and even hitting the return Ok(camp);
line... But I never get anything returned?
Fiddler:
[Fiddler] ReadResponse() failed: The server did not return a complete response for this request. Server returned 0 bytes.
Request Count: 1 Bytes Sent: 408 (headers:408; body:0) Bytes Received: 717 (headers:205; body:512)
ACTUAL PERFORMANCE
NOTE: This request was retried after a Receive operation failed.
ClientConnected: 14:20:36.281 ClientBeginRequest: 14:20:44.067 GotRequestHeaders: 14:20:44.067 ClientDoneRequest: 14:20:44.067 Determine Gateway: 0ms DNS Lookup: 0ms TCP/IP Connect: 0ms HTTPS Handshake: 0ms ServerConnected: 14:20:44.294 FiddlerBeginRequest: 14:20:44.294 ServerGotRequest: 14:20:44.294 ServerBeginResponse: 00:00:00.000 GotResponseHeaders: 00:00:00.000 ServerDoneResponse: 14:20:44.537 ClientBeginResponse: 14:20:44.537 ClientDoneResponse: 14:20:44.538
Overall Elapsed: 0:00:00.470
RESPONSE BYTES (by Content-Type) -------------- text/html: 512 ~headers~: 205
Environment:
Running on Windows 10, Visual Studio 2017, IIS Express, localhost port 8088, no SSL configured.
Controller:
using Microsoft.AspNetCore.Mvc;
using MyCodeCamp.Data;
namespace MyCodeCamp.Controllers {
[Route("api/[controller]")]
public class CampsController : Controller {
private readonly ICampRepository _campRepository;
public CampsController(ICampRepository campRepository) {
_campRepository = campRepository;
}
[HttpGet]
public IActionResult Get() {
try {
var camps = _campRepository.GetAllCamps();
return Ok(camps);
} catch { }
return BadRequest();
}
[HttpGet("{id}")]
public IActionResult Get(int id, bool includeSpeakers = false) {
try {
var camp = includeSpeakers
? _campRepository.GetCampWithSpeakers(id)
: _campRepository.GetCamp(id);
if (camp == null) {
return NotFound($"Camp with id '{id}' was not found.");
}
return Ok(camp);
} catch { }
return BadRequest();
}
}
}
~note When cloning from https://github.com/pluralsight-courses/Implementing-and-Securing-an-API-with-ASP.NET-Core/tree/master/Module02_ReadingData first do a dotnet restore
and in the MyCodeCamp.Data
folder make sure to execute dotnet ef database update
to init the database, otherwise you won't be able to run the sample if you want.
Ok,
This was my own fault - but for people hitting the same problem, I'm answering the question here as a reference, because the error I got (see my initial question) was not obvious :/.
Make sure you have ReferenceLoopHandling.Ignore
configured on the services.AddMvc
:
// Add framework services.
services.AddMvc()
.AddJsonOptions(opt => {
opt.SerializerSettings.ReferenceLoopHandling =
ReferenceLoopHandling.Ignore;
});
;