asp.net - Simple routing issue, optional not working -


i trying make lookup controller multiple actions. routing configuration is:

globalconfiguration.configuration.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional });  globalconfiguration.configuration.routes.maphttproute( name: "lookupsapi", routetemplate: "api/{controller}/{action}/{id}", defaults: new { id = routeparameter.optional  }); 

my lookup controller

[httpget] public lookupslist getallstates() {  }  [httpget] public lookupslist getallsources() { //method } 

when use following give nothing error stating "multiple actions found match request"

http://localhost:51042/api/lookups/getallstates or  http://localhost:51042/api/lookups/getallsources 

but when use

http://localhost:51042/api/lookups/getallstates/1 or  http://localhost:51042/api/lookups/getallsources/1 

it work fine.

how can set route work correctly.

thanks.

you have clash in routes think. example:

http://localhost:51042/api/lookups/getallstates 

will match first route.

you should reverse ordering of routes:

update edsf:

config.routes.maphttproute(     name: "lookupsapi",     routetemplate: "api/{controller}/{action}/{id}",     defaults: new {id = routeparameter.optional } );  config.routes.maphttproute(     name: "defaultapi",     routetemplate: "api/{controller}/{id}",     defaults: new { id = routeparameter.optional } ); 

additional helpful references:

  1. route order

  2. route params


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -