How to use Multiple POST GET DELETE in Web API Controller

If we are going to use multiple POST, GET or any HTTP verb in a single API controller, then it will throw an error due to ambiguity between the same verb methods . So, by considering this requirement, I have decided to write an article to explain why multiple Get and Post methods are not working and what routing configuration is required to use multiple GET and POST methods in a single Web API controller class.

All resources are accessed via the Base url+API+ApiController combination, which is defined by default in WebApiConfig.cs. For this reason, all resources are accessed using the controller name because inside the controller there is only one method defined which has the same HTTP verb like GET or POST . We can access the web API methods using the 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 the same url routing structure, then accessing the specific method is very difficult due to the ambiguity between the two same HTTP verb methods.

Solution

To overcome the preceding mentioned issue and allow multiple GET and POST methods in a single web API controller class, you need to modify the url structure which is defined inside the WebApiConfig.cs as shown in the following image.

Now, with the 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 the preceding examples and explanations, we have learned how to configure a web API routing template to use multiple GET POST methods in a single web API controller class.

Summary

I hope this article is useful for all readers. If you have any suggestions, then please mention them in the comment section.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode