我配置了這樣的路線:ASP.net網頁API 2錯誤「的參數字典包含參數無效項」
routes.MapHttpRoute("DefaultApi2", "api/{controller}/{action}/{id}", new {id = RouteParameter.Optional}
和我的控制器看起來是這樣的:
public class RoutineController : ApiController
{
private readonly RoutineService _routineService;
public RoutineController(RoutineService routineService)
{
_routineService = routineService;
}
[HttpGet]
[ActionName("Tags")]
public List<RoutineTag> Tags()
{
return _routineService.GetAllTags();
}
[HttpGet]
[ActionName("SingleRoutine")]
// GET api/routine/5
public RoutineViewModel SingleRoutine(int id)
{
return _routineService.GetRoutineById(id);
}
}
但如果我改變方法SingleRoutine這個
{"Message":"The request is invalid.","MessageDetail":"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'Infrastructure.Api.Models.RoutineViewModel Routine(Int32)' in 'Infrastructure.Api.Controllers.RoutineController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."}
:我得到這個錯誤
[HttpGet]
[ActionName("SingleRoutine")]
// GET api/routine/5
public RoutineViewModel SingleRoutine(int? id)
{
if (!id.HasValue)
{
return null;
}
return _routineService.GetRoutineById((int) id);
}
在瀏覽器中我只看到「null」。
這是怎麼發生的?
EDIT
當我輸入/ API /例程/標籤
錯誤在哪一行發生?我敢打賭它不在這裏。 –
異常不會拋出,這個錯誤顯示在瀏覽器 – hyperN
我不知道它是否重要,但我使用Niject作爲IoC – hyperN