这真让我抓狂。这是我的控制器动作:
[HttpGet]
public List<Product> RetrieveByIds(int[] ids)
{
Product p = new Product();
//return p.RetrieveByIds(ids);
return new List<Product>();
}
这是我的路由文件:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ActionApiMultipleIds",
routeTemplate: "api/{controller}/{action}/{ids}",
defaults: new { ids = RouteParameter.Optional }
);
}
当我尝试使用 RestConsole 对其进行测试时,我不断收到此错误:
{
"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.Collections.Generic.List`1[GrownHome.Web.Models.Product] RetrieveAllByUserId(Int32)' in 'GrownHome.Web.Controllers.Api.ProductController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."
}
我一直盯着我的屏幕摆弄代码,但我一无所获。我不必向你们解释这是多么令人沮丧:)))
我时不时地遇到这类路由问题,我似乎无法解决这类问题。一些见解将非常受欢迎!
谢谢!