Questions

Answer the following questions to test the knowledge we have gained in this chapter:

  1. We have a class that we want to register for dependency injection and want a new instance of it to be created when injected into a class. What method in IServiceCollection should we use to register the dependency?
  2. In a controller action method, if a resource can't be found, what method can we use in ControllerBase to return status code 404?
  3. In a controller action method to post a new building, we implement some validation that requires a database call to check whether the building already exists. If the building does already exist, we want to return HTTP status code 400:
[HttpPost]
public ActionResult<BuildingResponse> PostBuilding(BuildingPostRequest buildingPostRequest)
{
var buildingExists =
_dataRepository.BuildingExists(buildingPostRequest.Code);
if (buildingExists)
{
// TODO - return status code 400
}
...
}

What method from ControllerBase can we use to return status code 400?

  1. The model for the preceding action method is as follows:
public class BuildingPostRequest
{
public string Code { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}

We send an HTTP POST request to the resource with the following body:

{
"code": "BTOW",
"name": "Blackpool Tower",
"buildingDescription": "Blackpool Tower is a tourist attraction
in Blackpool"
}

The Description property in the model isn't getting populated during the request. What is the problem?

  1. In the preceding request model, we want to validate that the code and name fields are populated. How can we do this with validation attributes?
  2. What validation attribute could we use to validate that a number property is between 1 and 10?
  3. What Http attribute could we use to tell ASP.NET Core that an action method handles HTTP PATCH requests?

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

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