Understanding Service Routing

As you saw with the method Get(id) in the CustomerController class, service methods in ASP.NET Web API are accessed via a URL routing convention. This convention works in a similar way to the routing in ASP.NET MVC. The biggest difference is that Web API leverages the HTTP verb to select the appropriate action in your controller.

A request is sent to your website, parsed for the HTTP verb (GET, POST, PUT, DELETE), and processed through a routing engine. The routing engine uses conventions to find your controller and map the request based on verb and parameters to the appropriate action method. Recall that we set the route for the CustomerController class at the top of the class using the attribute [Route("api/[controller]")]. This is the default convention for Web API.

The api/ portion of the construct is a convention indicating that this request is meant for a service. The [controller] portion of the route definition is used to find your controller based on the URL. In the example you have been creating, this is customer. (The word controller is not necessary.) You can then append parameter values to the request, as in /api/customer/1.

ASP.NET uses the api/ portion of the route to avoid collision with your other controllers. For example, you might want to route requests for /customer/1 to a web page to display a customer where the customer id=1. This allows a request to /api/customer/1 on the same domain to service customer data interactions with your API. Of course, this is easily changed if you do not like the convention. Just mark your service controller route attribute as [Route("[controller]")]; this eliminate the api/ portion of the convention.

Include the Action in the Route

Sometimes your service includes multiple definitions that use the same HTTP verb and accept the same parameter types. For example, you may have GetCustomer(int id) and GetCustomerOrder(int id) in the same service controller. In this case, using the default routing convention of api/[controller] will result in an error because the request is ambiguous and cannot be satisfied. Figure 19.10 shows the error in the browser.

Image

FIGURE 19.10 Two service methods in the same controller with the same HTTP verb and parameters will result in a server error (unless you change the route convention to include the action).

You can solve this problem by changing the route convention to include the action method. You do so by editing the Route attribute at the top of the class as follows: [Route("api/[controller]/[action]")]. This changes the convention to require the action name.

Notice that the route is now mapped to your method name. ASP.NET Web API also gives you control of your action names for the route. To change the action name (without renaming your methods), you add the ActionName attribute class. For example, you might use [ActionName("Get")] for the GetCustomer method and [ActionName("GetOrder")] for the GetCustomerOrder method. Doing so changes your URL route; now to get a customer, you would call /api/customer/get/1.

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

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