Reputation: 43
I am experimenting with WebApi and have created a controller with two methods.
I started with the following method first:
[HttpGet]
[Route("car/{registration}")]
public object GetCarByRegistration(string registration) {
return null;
}
When debugging I put a breakpoint on return null;
tested url http://localhost:51245/api/car/yw25jdk
which work fine, visual studio stopped at my breakpoint and the registration
variable was the same value in the url.
But when I added the following method:
[HttpGet]
[Route("car/{serial}")]
public object GetCarBySerial(string serial) {
return null;
}
The first url stopped working and I started to get 500 - Internal Server Error
. If I take the second method out then the first method works again.
I cannot understand why the second method breaks the first one.
Can someone explain this to me please?
Upvotes: 4
Views: 5493
Reputation: 321
Well thats because ASP does not know which method to use when you go to the url http://localhost:51245/api/car/yw25jdk
since both methods say they expect a string as their parameter.
How should ASP know the difference between /car/{registration}
and /car/{serial]
, since both of them are string?
You should change the Route of on of them, to get it working
Upvotes: 7
Reputation: 635
Put this in your WebApiConfig:
config.MapHttpAttributeRoutes();
Otherwise WebApi ignores the Route attribute. And without the different route signatures, when the ApiController receives a GET request, it automatically looks for the first action that starts with 'Get' - this is why it works when you remove the second method
Upvotes: 0