Evgeni
Evgeni

Reputation: 3353

WebAPI routing - how to make default routing ignore named methods?

Say I have those methods in a controller:

Get() 

[HttpGet]
FindSomeone()

I have a default route, and a route with actions: routeTemplate: "api/{controller}/{action}".

Works fine when I call /mycontroller/FindSomeone, but fails with multiple matching routes error when I call GET /mycontroller/.

Is there a way to make default route to match Get() method only, and skip the FindSomeone() method?

Upvotes: 0

Views: 1070

Answers (2)

Pablo Cibraro
Pablo Cibraro

Reputation: 3959

AttributeRouting migh work better for you in this scenario. It should be as simple as decorating the two methods with a Get Attribute,

[RoutePrefix("mycontroller")
public class MyController 
{
  [GET("")]
  Get() 

  [GET("FindSomeone")]
  FindSomeone()
}

That will make those methods available as mycontroller and mycontroller/FindSomeone.

Upvotes: 0

Giorgio Minardi
Giorgio Minardi

Reputation: 2775

Declare the default Get action for all controllers in the main routing

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional, action ="Get" }
    );

Upvotes: 0

Related Questions