Creating an action method for posting a question

Let's implement an action method for posting a question:

  1. We'll start with the skeleton method:
[HttpPost]
public ActionResult<QuestionGetSingleResponse>
PostQuestion(QuestionPostRequest questionPostRequest)
{
// TODO - call the data repository to save the question
// TODO - return HTTP status code 201
}

Note that we use an HttpPost attribute to tell ASP.NET Core that this method handles HTTP POST requests.

Note that the method parameter type is a class. Earlier, in the Extending the GetQuestions action method for searching section, we introduced ourselves to model binding and explained how it maps data from an HTTP request to method parameters. Well, model binding can map data from the HTTP body as well as the query string. Model binding can also map to properties in parameters. This means that the data in the HTTP request body will be mapped to properties in the instance of the QuestionPostRequest class.

  1. Let's call into the data repository to post the question:
[HttpPost]
public ActionResult<QuestionGetSingleResponse>
PostQuestion(QuestionPostRequest questionPostRequest)
{
var savedQuestion =
_dataRepository.PostQuestion(questionPostRequest);


// TODO - return HTTP status code 201
}
  1. The last step in the implementation is to return status code 201 to signify that the resource has been created:
[HttpPost]
public ActionResult<QuestionGetSingleResponse>
PostQuestion(QuestionPostRequest questionPostRequest)
{
var savedQuestion =
_dataRepository.PostQuestion(questionPostRequest);
return CreatedAtAction(nameof(GetQuestion),
new { questionId = savedQuestion.QuestionId },
savedQuestion);

}

We return a call to the CreatedAtAction from ControllerBase, which will return status code 201 with the question in the response. In addition, it also includes a Location HTTP header that contains the path to get the question.

  1. Let's try this out by running the app. This time we'll use Postman, which is a great tool for testing REST APIs. Open Postman, set the HTTP method to POST, enter the path to the questions resource, and add an HTTP header called Content-Type with a value of application/json:

  1. Enter a request body and click the Send button to send the request:

The expected 201 HTTP status code is returned with the saved question in the response.

Note how the question in the response has the generated questionId, which will be useful for the consumer when interacting with the question.

  1. If we look at the response headers, we can see that ASP.NET Core has also included a Location HTTP header that contains the path to get the question:

 That's a nice touch.

  1. Stop our app running so that we are ready to implement another action method.

That completes the implementation of the action method that will handle POST requests to api/questions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.68.81