Skip to main content

WebAPI - Simple-Attribute-Routing - Basic

My photo
Attribute routing is very helpful when you are trying to customize the response behavior of your RESTful application. For this article example, we will take advantage of the Attribute Routing by performing some filtering at the back end by decorating the method in the controller. Below are the code to show the implementation. Notice the route attribute is different for each method. Not just to route attribute is different, we also may pick any arbitrary method name that is following naming convention in our development environment. Take note for this example, we make use of Authorization attribute.

Based on the code, we may now able to give the caller some options on how the response going to be. Assuming the caller is a Manager, which hold the role for HR and Sales. The caller (manager) will be able to make use of the Attribute Routing to pick which response he/she would prefer.If he/she would like to :

1.Get all public data - The Manager may call
http://localhost:26374/api/publicaccess/65
2.Get data related to HR - The Manager may call
http://localhost:26374/api/publicaccess/65/hr
3.Get data related to Sales - The Manager may call
http://localhost:26374/api/publicaccess/65/sales
Conclusion

Based on the example, with Web API 2 we now may perform some sort like a basic filtering on how to response to client calls.

Back - Web Application > Web Api

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

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