I'm a tad confused - I've created an ASP.NET Core Web API MVC Project but when i try to make a request i am getting the following response:
I am posting to https://localhost:44337/api/Solve with Postman the following body:
{
"examId":"0f537776-1acf-478f-82ee-c8476bc3e005",
"selectedAnswers":
[
{
"id":"9163fd1c-ec0f-4f1f-8ead-05ffeac36426",
"answerText":"Yes",
"isCorrect":true
},
{
"id":"00545a13-212b-46a5-9d06-3f6abbb9f1d8",
"answerText":"Yes",
"isCorrect":true
}
]
}
and receive this as a response:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "Bad Request",
"status": 400,
"traceId": "8000005f-0001-ff00-b63f-84710c7967bb"
}
I've already included the Content-Type. GlobalConstants.RouteConstants.ApiRoute = "api/" GlobalConstants.RouteConstants.PostSolve = "Solve" Here is my controller:
[Route(GlobalConstants.RouteConstants.ApiRoute)]
[ApiController]
public class ESchoolController : ControllerBase
{
protected IApiService ApiService { get; set; }
public ESchoolController(IApiService apiService)
{
this.ApiService = apiService;
}
//POST: api/Solve
[HttpPost]
[Route(GlobalConstants.RouteConstants.PostSolve)]
public IActionResult Solve([FromBody]ExamApiSolveInputModel model)
{
if (!this.ModelState.IsValid)
{
return this.BadRequest();
}
try
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var result = this.ApiService.SolveExam(model, userId);
return this.Ok(result);
}
catch (Exception e)
{
return this.BadRequest(e.Message);
}
}
}
Here are my input models:
public class ExamApiSolveInputModel
{
public ExamApiSolveInputModel()
{
this.SelectedAnswers = new List<AnswerApiInputModel>();
}
public string ExamId { get; set; }
public ICollection<AnswerApiInputModel> SelectedAnswers { get; set; }
}
public class AnswerApiInputModel
{
public string Id { get; set; }
public string AnswerText { get; set; }
public bool IsCorrect { get; set; }
}
I've been searching for solution, but unsuccessfully. I've tried some things like:
- When i make request it's not entering the controller. I've checked it with the debugger.
- Making another post method but again it's not entering the code via debugger, so I think the problem isn't in the code.
Any ideas how to solve this problem? Thanks alot and happy holidays!
User.FindFirstValueis. Its probably the ApiService. Can you post that class, or at least the methods used inSolveExam? I've copy and pasted your models and controller into a new project and commented out theApiServiceandUserreferences and I am getting 200 OK. One of those is throwing an exception. Use your debugger to step through a request and see where the exception is thrown.