0

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 ?

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51

3 Answers3

1

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?}"]
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • 2
    I would note that solving this issue this way can get really tedious, and that instead it should be solved by adding actions to the route template or by using `[Route]` annotations on each controller method. Just my personal thought, though – Mark C. Dec 28 '16 at 17:43
  • Yeah, you are right. It's terrible to add new route for each method. – Roman Marusyk Dec 28 '16 at 17:45
  • 1
    Also denote in your answer that `[HttpGet("{VariableComplexRef}")]` is ASP.NET Core syntax. In previous versions it is still `[HttpGet]` and `[Route("/yourRoute")]` – Mark C. Dec 28 '16 at 17:56
  • Thanks a lot. I've fixed it. – Roman Marusyk Dec 28 '16 at 18:11
1

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).

Arturo Menchaca
  • 15,783
  • 1
  • 29
  • 53
0

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

M B
  • 2,326
  • 24
  • 33