Anu Viswan
Anu Viswan

Reputation: 18163

Get Request and APIs with/without parameter

I have two APIs,

[HttpGet]
public bool WithoutParamBooleanResponse()

and

[HttpGet]
public string ComplexReferenceTypeParamStringResponse([FromUri]ComplexRefType VariableComplexRef)

However, this leads to having error

multiple actions were found that match the request web api get.

If I were to add another dummy parameter for the second method, the whole thing works. Could someone explain why a parameterless method and a method with a complex parameter are seen similar by the API ?

Upvotes: 0

Views: 4621

Answers (3)

Roman Marusyk
Roman Marusyk

Reputation: 24619

Try to create a new route like:

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

and try to add attribute on your action

[Route("ComplexReferenceTypeParamStringResponse/{VariableComplexRef?}"]

Upvotes: 1

Arturo Menchaca
Arturo Menchaca

Reputation: 15982

why a parameterless method and a method with a complex parameter are seen similar by the API ?

When a parameter is annotated with FromUri attribute and is a complex type, the value is constructed from the query params, therefore the route for both methods would be the same (since the query params are not taken into account).

Upvotes: 1

M B
M B

Reputation: 2326

You need to add an action to your routing url.

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

When calling a route and only passing in a controller, the routing assumes there is only one action for each method(GET,POST..) and looks for it. This is why you are having an error with more than one GET.
When you also pass an action, it is more specific to look for the correct action with this method

Upvotes: 0

Related Questions