###BeginCaseStudy###
Topic 3, Online Bookstore
Background
You are developing an online bookstore web application that will be used by your company’s customers.
Technical RequirementsGeneral requirements:
The web store application must be an ASP.NET MVC application written
in Visual Studio.
The application must connect to a Microsoft SQL database.
The GetTop100Books() method is mission critical and must return data
as quickly as possible. It should take advantage of fast, forward-only, read-only
methods of reading data.
The ImportBooks() method must keep a copy of the data that can be
accessed while new books are being imported without blocking reads.
The Create MonthlyTotalsReport() method must lock the data and
prevent others from updating or inserting new rows until complete.
The college textbook area of the web application must get data from a
daily updated CSV file.
The children’s book area of the web application must get data directly
from a local database. It must use a connection string. It must also support
access to the stored procedures on the database. Further, it is required to have
strongly typed objects. Finally, it will require access to databases from multiple
vendors and needs to support more than one-to-one mapping of database
tables.
The cookbook functionality is contained within a client-side application
that must connect to the server using HTTP and requires access to the data
using JavaScript.
The BookApiController class must have a method that is able to perform
ad-hoc queries using OData.
The RESTful API of the bookstore must expose the following endpoints.
Application Structure
PurchaseOrders.xml
FeaturedBooks.xml
###EndCaseStudy###
You need to configure the server to self-host the bookstore’s Web API application.
Which code segment should you use?
A.
Option A
B.
Option B
C.
Option C
D.
Option D
Explanation:
MapHttpRoute Method
Maps the specified route template.
Use the option with “api/…
D
Relative uri does not have “api” in this context
books
books/id
D has invalid last string – server.Wait().OpenAsync(), should be server.OpenAsync().Wait().
none of these can be correct
the relative uri has to be books and books/id
this would mean we would have 2 options:
-{controller}/{id} and the controller name would be BooksController
-{controller}s/{id} and the controller name would be BookController
but the name of the controller is BookApiController making the relative uri bookapi/id
if anything option B is the closest
options A and C are definetly there to trick people
+1
agree
none of these can be correct
option B is the closest
B
Correct C!
https://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
B is correct. C is wrong because the relative URL has not api in this context
Correct answer is C as the page said.
Here are the reasons:
1. A is incorrect because congfig.Routes.MapHttpRoute is correct, NOT config.Filters.Add. Also this command doesn’t exist – server.Wait().OpenAsync()
2. D is incorrect because you need “api/” since this is a Web API. Without “api/”, it would use MVC Routing. The api/ distinguishes between MVC routing and Web API routing. Also “server.Wait().OpenAsync()” doesn’t exist. It has to be “server.OpenAsync().Wait()”;
As “AAA” said: https://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
3. B and C is almost identical but C has api/ in the URL and C does not have api/.
4. Answer is C because you need api/ since the question is asking you for the WEB API routing not MVC application routing. You need to configure the self-hosted Web Api.
The controller is Book. So api/{controller}s/id would be api/books/id. Without the “s” in /api/{controller}s/id, it would be api/book/id which is wrong. But it has the “s” so it is correct.
C is the correct answer.
+1 C
I have to disagree:
– Tested with VS2015, result (Controllers/BookApiController with WebApiConfig same as in C):
No HTTP resource was found that matches the request URI ‘http://localhost:12345/api/books’.
No type was found that matches the controller named ‘book’.
If I use correct address (http://localhost:12345/api/bookapis):
value1
value2
But C is closest right.
First of all “api” part is not mandatory but because it is part of ASP.NET MVC + WEB API it makes sense to use “api”. Still, I don’t think this question is valid because you can use both “{controller}s/{id}” and “api/{controller}s/{id}”, it’s up to you which one you’d like to use. So, C but B is also possible.