Allow Multiple GET POST Methods in Single ASP.NET Web API Controller

Many reader asking me, How we use multiple GET, POST , PUT , Delete methods in single Web API Controller class since , If we are going use multiple POST or GET methods in single API controller then its not working . So by considering this requirement I have decided to write article to explain why multiple Get , Post methods are not working and what configuration we need to do So we can use multiple Get , POST methods in single Web API controller class.
Problem
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 .
Solution
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.
E.g
http://localhost:56290/api/Home/AddEmployeeDetails 
From all preceding examples and explanations we have learned how to use multiple GET POST methods in single web API controller class.
Summary
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

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode