Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method with two parameters in asp.net web api

How can I make a method with two parameters using ASP.NET Web Api?

So that I can call it like localhost/controller/param1/param2

like image 751
Bart Avatar asked Mar 13 '12 09:03

Bart


People also ask

How do I pass multiple complex type in Web API?

Best way to pass multiple complex object to webapi services is by using tuple other than dynamic, json string, custom class. No need to serialize and deserialize passing object while using tuple. If you want to send more than seven complex object create internal tuple object for last tuple argument.

Can we have multiple post method in Web API?

Web API provides the necessary action methods for HTTP GET, POST, PUT, and DELETE operations. You would typically pass a single object as a parameter to the PUT and POST action methods. Note that Web API does not support passing multiple POST parameters to Web API controller methods by default.


2 Answers

You can also call the url with specific params names in the querystring:

/api/actions?param1=5&param2=1/1/2000 

Then the controller method would be:

GetByParams(int param1, DateTime param2) 
like image 175
Nadav Lebovitch Avatar answered Oct 04 '22 08:10

Nadav Lebovitch


Just change or add route in global.asax

routes.MapHttpRoute(name: "DefaultApi1", routeTemplate: "api/{controller}/{id}/{name}", Defaults: new{} ); 
like image 33
Denis Agarev Avatar answered Oct 04 '22 07:10

Denis Agarev