Reputation: 51094
I have a single controller with the two following action signatures. I'm using convetional vs. attribute routing because I've been trying anything to make this work, and conventional removed 1 of 2 problems.
[HttpGet]
[ResponseType(typeof(IEnumerable<EmployeeBindingModel>))]
public async Task<IHttpActionResult> ReadAll()
When I call this from Postman with the URL: http://localhost:53093/api/Employees/ReadAll
I get the following JSON back:
{
"Message": "The request is invalid.",
"MessageDetail": "The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Threading.Tasks.Task`1[System.Web.Http.IHttpActionResult] Read(Int32)' in 'AcmeSoft.WebApi.Controllers.EmployeesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."
}
The other action is:
[HttpGet]
[ResponseType(typeof(EmployeeBindingModel))]
public async Task<IHttpActionResult> Read(int id)
and when I call it with this URL: http://localhost:53093/api/Employees/Read/5
I get the expected employee JSON record back.
My routing is set up as follows in WebApiConfig
:
//config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new {id = RouteParameter.Optional});
I've always used this standard boilerplate, and today is the first time I get the error. Why on Earth would an action without a parameter expect one? ReadAll
has never had a parameter, so it's not even some ghost. I've deleted all ASP.NET Temporary files with no change.
Upvotes: 0
Views: 942
Reputation: 7079
Make change in WebApiConfig:
//config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new {id = RouteParameter.Optional});
Use action also in routeTemplate
Upvotes: 2