...
/Creating an Action Method for Updating a Question
Creating an Action Method for Updating a Question
Learn to create an action method for updating a question.
We'll cover the following...
Implementing action method for updating a question
Let’s move on to updating a question. To do that, implement the following steps:
1. Add the following skeleton for the action method:
Press + to interact
[HttpPut("{questionId}")]public ActionResult<QuestionGetSingleResponse>PutQuestion(int questionId,QuestionPutRequest questionPutRequest){// TODO - get the question from the data// repository// TODO - return HTTP status code 404 if the// question isn't found// TODO - update the question model// TODO - call the data repository with the// updated question model to update the question// in the database// TODO - return the saved question}
We use the HttpPut
attribute to tell ASP.NET that this method handles HTTP PUT
requests. We are also putting the route parameter for the question ID in the questionId
method parameter.
The ASP.NET model binding will populate the QuestionPutRequest
class instance from the HTTP request body.
2. Let’s get the question from the data repository and return HTTP status code 404
if the question isn’t found:
Press + to interact
[HttpPut("{questionId}")]public ActionResult<QuestionGetSingleResponse>PutQuestion(int questionId,QuestionPutRequest questionPutRequest){}var question = _dataRepository.GetQuestion(questionId);if (question == null){return NotFound();}// TODO - update the question model// TODO - call the data repository with the// updated question//model to update the question in the database// TODO - return the saved question}
...