Home » Microsoft » 70-487 v.2 » Which code segments should you include in Target 1, Target 2, Target 3, Target 4 and Target 5 to complete the code?
DRAG DROP
You are developing an ASP.NET Web API application.
The methods of the Web API must return details about the result of the operation.
You need to create methods to update and delete products.
You have the following code:
Which code segments should you include in Target 1, Target 2, Target 3, Target 4 and Target 5 to complete the code? To answer, drag the appropriate code segments to the correct targets in the answer area. Each code segment may be used once, more than once, or not at all. You may need to drag the split bar between panes or scroll to view content.
NOTE: Each correct selection is worth one point.
Select and Place:
Correct Answer:
Explanation/Reference:
Explanation:
Box 1: HttpResponseException
Box 2: HttpResponseMessage Box 3: NoContent
For more control over the response, you can construct the entire response message and include it with the HttpResponseException. Example:
public Product GetProduct(int id)
{
Product item = repository.Get(id); if (item == null)
{ var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No product with ID = {0}", id)), ReasonPhrase = "Product ID Not Found"
};
throw new HttpResponseException(resp);
}
return item; }
Box 4: HttpResponseMessage
Box 5: NotFound
The HttpError object provides a consistent way to return error information in the response body. The following example shows how to return HTTP status code
404 (Not Found) with an HttpError in the response body. public HttpResponseMessage GetProduct(int id)
{
Product item = repository.Get(id); if (item == null)
{ var message = string.Format("Product with id = {0} not found", id); return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
} else
{
return Request.CreateResponse(HttpStatusCode.OK, item);
}
}
CreateErrorResponse is an extension method defined in the System.Net.Http.HttpRequestMessageExtensions class. Internally, CreateErrorResponse creates an HttpError instance and then creates an HttpResponseMessage that contains the HttpError.
References: https://docs.microsoft.com/en-us/aspnet/web-api/overview/error-handling/exception-handling
No, answer is correct:
NotFound
Equivalent to HTTP status 404.NotFound indicates that the requested resource does not exist on the server.
NoContent
Equivalent to HTTP status 204.NoContent indicates that the request has been successfully processed and that the response is intentionally blank.
https://msdn.microsoft.com/es-es/library/system.net.httpstatuscode(v=vs.110).aspx
I mean the official answer is NOT correct.
3. NotFound
5. NoContent
Switch NoContent and NoFound