Bart
Bart

Reputation: 2341

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

Upvotes: 26

Views: 35088

Answers (3)

Nerdroid
Nerdroid

Reputation: 13966

I think the easiest way is to simply use AttributeRouting.

[Route("api/YOURCONTROLLER/{paramOne}/{paramTwo}")]
public string Get(int paramOne, int paramTwo) {
    return "The [Route] with multiple params worked";
}

The {} names need to match your parameters.

Attribute Routing in ASP.NET Web API 2

Upvotes: 7

Denis Agarev
Denis Agarev

Reputation: 1529

Just change or add route in global.asax

routes.MapHttpRoute(name: "DefaultApi1", routeTemplate: "api/{controller}/{id}/{name}", Defaults: new{} );

Upvotes: 7

Nadav Lebovitch
Nadav Lebovitch

Reputation: 690

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)

Upvotes: 57

Related Questions