Friday, 5 August 2016

Restful WebApi Routing | Customizing WebApi Routes

In this article, I would show conventional and attribute based routing. Open WebApiConfig.cs file add new http route as shown below, just to avoid conflict between each route, removed api in the 2nd route table. Framework hierarchically traverse through route table till it finds the match. We should make sure to arrange the route properly to avoid conflicts.
Below screen shot shows the routing of api using ProductAPI and DefaultAPI. Both render same result its just the urls are different.
Lets do a PUT operation using ProductAPI routing table.
Coming back to product controller, lets define the HTTP Verb and ActionName as shown below
Now WebApi operations can be performed using the actionname instead of method names.
Configuring Attribute based routing. I added Route attribute to each method as shown below.
Now, WebApi operations are performed using the route name.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.