Skip to main content

WebAPI - Create A Direct Route - Basic


Back - Web Application > Web Api

There are 2 places that we may decide on how to handle our routing, although there are more sophisticated technique such as custom attribute, in this article we will discuss on 2 of the basic way to implement ASP.NET Web API routing.

1. At The Method
As we can see from the code snipped above, we may decorate the method with a direct routing ( Line 3,8,13 ). The caller may consume it by making a call for example :
1. Return Individual Invoice - http://localhost:xxxxxx/api/invoice/yyyy
where the 'xxxxxx' is the port and 'yyyy' is the invoice id.
2. Return All Invoice List - http://localhost:xxxxxx/api/invoices
3. Return Items In Invoices From Invoice ID - http://localhost:xxxxxx/api/invoice/yyyy/items
where the 'xxxxxx' is the port and 'yyyy' is the invoice id.

2. At The Controller
The code above is a simple web api controller that implement 3 methods. The controller is decorated with a route prefix attribute ( Line 1 ). Notice at the methods, we may customize the direct routing by adding route attribute at each of our methods.

Conclusion
We may customize our routing using 2 basic technique, This technique is not an ideal solution but enough to show the features of Asp.Net Web API. There technique is error prone if the production solution might have a lot of customized method that is implemented in a specific controller since the intellisense might not be able to capture the string that we provide in the attributes.

Back - Web Application > Web Api

Published on : 12-Jan-2018
Ref no : DDN-WPUB-000036

About Author

My photo
Wan Mohd Adzha MCPD,MCSD,MCSE
I am passionate about technology and of course love Durians. Certified by Microsoft as MCP Since 2011. Blogging from Malaysia

Comments