0

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!

10
  • 400 is invalid request. Without minimal reproducible example showing how you make the request no one will be able to help. Commented Dec 27, 2019 at 20:41
  • What would you want to know? Commented Dec 27, 2019 at 20:49
  • @AlexeiLevenkov when i try with empty controller it also says 400 invalid request Commented Dec 27, 2019 at 20:52
  • 1
    Either your ApiService is throwing an exception, or User.FindFirstValue is. Its probably the ApiService. Can you post that class, or at least the methods used in SolveExam? I've copy and pasted your models and controller into a new project and commented out the ApiService and User references 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. Commented Dec 27, 2019 at 21:37
  • @Josh it cannot break in the controller, it's just skipping it Commented Dec 27, 2019 at 22:26

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

If you want to keep that for Mvc requests but not ApiController requests you can use the [IgnoreAntiforgeryToken] attribute
0

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"

3 Comments

what values do you have in these constants: GlobalConstants.RouteConstants.ApiRoute and GlobalConstants.RouteConstants.PostSolve ?
GlobalConstants.RouteConstants.ApiRoute = "api/" GlobalConstants.RouteConstants.PostSolve = "Solve"
something else going on there. I created test core 3.0 api using code you provided and making post request using postman and json from example, works like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.