Configuring Routing Template for Multiple POST GET DELETE in Single Web API Controller

If we are going use multiple POST, GET or any HTTP verbs in single API controller then it will through the error due to ambiguity between the same verb methods . So by considering this requirement I have decided to write article to explain why multiple Get , Post methods are not working and what routing configuration required to use multiple GET , POST methods in single Web API controller class.
The default URL structure of ASP.NET Web API defined into the WebApiConfig.cs file is like shown in the following Image.

In the Preceding routing structure, we can access the resource using Base url+API+ApiController which is by default defined in WebApiConfig.cs due of this reason all resources accessed using controller name because inside the controller not more than one method defined which having same HTTP verb like GET or POST . We can access the web API methods using following url structure with this default routing pattern .
 Consider our REST Service URL will be http://localhost:56290/api/Home which is described as follows,
  • http://localhost:56290: Is the base address of web API service, It can be different as per your server.
  • API: It is the used to differentiate between Web API controller and MVC controller request .
  • Home: This is the Web API controller name.
Once we add another POST or Get method inside the API controller class with same url routing structure then while accessing the specific method is very difficult due to the ambiguity between the two same HTTP verb method .
To overcome the preceding mentioned issue and allow multiple GET , POST methods in single web API controller class then you need to modify url structure which is defined inside the WebApiConfig.cs as shown in the following Image .

Now with preceding url pattern we can access web API methods base url +API+Controller+webApi method.
From all preceding examples and explanations we have learned how to configure web API routing template to use multiple GET POST methods in single web API controller class.
I hope this article is useful for all readers. If you have any suggestions, then please mention it in the comment section.

Post a Comment

Protected by Copyscape Online Copyright Protection
Copyright © Compile Mode