exam questions

Exam 70-487 All Questions

View all questions & answers for the 70-487 exam

Exam 70-487 topic 1 question 74 discussion

Actual exam question from Microsoft's 70-487
Question #: 74
Topic #: 1
[All 70-487 Questions]

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.
Each correct selection is worth one point.
NOTE:
Select and Place:

Show Suggested Answer Hide Answer
Suggested Answer:
Box 1: HttpResponseException -

Box 2: HttpResponseMessage -

Box 3: NotFound -
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: NoContent -
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

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
MalaSvinjica
5 years ago
I think box 5 should be NotFound
upvoted 1 times
JftCoCo
4 years, 11 months ago
The answer to box 5 "NoContent" is right.
upvoted 12 times
MalaSvinjica
4 years, 11 months ago
Yes, you are right:)
upvoted 6 times
...
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...