Valentin Trifonov
Valentin Trifonov

Reputation: 11

ASP.NET Core Web API Bad Request

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:

  1. When i make request it's not entering the controller. I've checked it with the debugger.
  2. 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!

Upvotes: 0

Views: 2719

Answers (2)

Valentin Trifonov
Valentin Trifonov

Reputation: 11

I removed the services.AddMvc(options => services.AddMvc(options => options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute())); from the Startup.cs file and the problem disappeared!

Upvotes: 1

Roman.Pavelko
Roman.Pavelko

Reputation: 1655

You need to provide more information how do you make your request.

Make sure you do include your JSON in body and set Content-Type header as "application/json"

Upvotes: 0

Related Questions