Skip to main content

WebAPI - Custom Routing With RoutePrefixAttribute

My photo
Web Api is a good platform to assists in data access strategy for mobile solutions or n-tier architecture. Problem always arise when we need to customize our demanding clients caller. With regards our client needs, we may take advantage of RoutePrefixAttribute to perform the task. This is by making use of the routing customization that Web API has to offer. The client may adjust their needs based on their Uri and make a call .
The Requirements
1. We have IOs clients that require Json data, despite there are many versions, each version return different set of data.
2. We also have an Application that is very customized that require a custom XML schema data type and have many version that are calling from RedHat server.

This article will make use of RoutePrefixAttribute to solve the requirements. Yes, there are many other way to solve and meet the requirements, this solution might be a suitable choice and show the capability of Web API 2. Note that all the customized requirement is put behind, this article is based on its title where we want to point out the capability of the RoutePrefixAttribute
The Step
1. Create the 'Attribute-Component' which is a class that inherit from RoutePrefixAttribute
2. Decorate the controller with the Attribute

Step 1 - Create the 'Attribute-Component' which is a class that inherit from RoutePrefixAttribute

As we can see from the code, we started by creating a class name 'Version' that is based on RoutePrefixAttribute and the constructor will receive 3 parameter as an arguments. In this case this 3 variable is what define the route where.
Variable 1 - Define the Application Caller Name ( or the namespace ) .
Variable 2 - Define the data type or the consumer type.
Variable 3 - Define the versioning of the client required.

Step 2 - Decorate the controller with the Attribute

As we can see the attribute implementation is very simple, decorate it at the controller and name the controller with what ever name that comfortable with the developers in the team environment. We leave behind the implementation inside the method of the controller. But here lets express our concern on the CustomRouteAttribute. Notice that the third arguments of the first controller which is '4.2.3' . We are using a [dot] there. This is where the beauty is. Web API routing engine will treat it as a string. Lets show an example of the call .

Targetting Method No 1.
http://localhost:26374/api/app-1/ios-json/4.2.3/getclients


Targetting Method No 2.
http://localhost:26374/api/app-1/xml-bb/1-1-2/getclients


Conclusion

From the article we may summarize that there are many ways to help us versioning out RESTful services, using RoutePrefixAttribute is one of the solution.

Back - Web Application > Web Api

Published on : 13-Jan-2018
Ref no : DDN-WPUB-000040

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