On my Web API I have a Document Controller with two simple actions:
[AllowAnonymous]
public class DocumentController : ApiController
{
public String Get(int id)
{
return "test";
}
public String Get(string name)
{
return "test2";
}
}
The following URL (executes the first function) works fine:
But this URL (should execute the second function):
Throws this error:
{ "message": "The request is invalid.", "messageDetail": "The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'xx.yy.Document Get(Int32)' in 'API.Controllers.DocumentController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter." }
This is the MapHttpRoute
in the WebApiConfig:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
What am I doing wrong? Please advise.