这是推动我疯了。这是我的控制器操作:不了解正确的方法来路由我的行动
[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."
}
我保持主演在我的屏幕上摆弄代码,我无处可去。我不必向你们解释这是多么令人沮丧:)))
我偶尔遇到这种类型的路由问题,我似乎无法解决这类问题。有些见解会非常受欢迎!
谢谢!因为一个前捕获所有具有相同的格式要求
请注意,您的设置中的第三个也不会被命中。解决办法是对两者都有约束或使用相同的路线。此外,它也不会打第二个如果添加一个id,因为这将通过{}行动在以前的一个被抓(IDS设置为可选)。 – JuhaKangas