我有2种操作方法,像这样的WebAPI控制器:重载的WebAPI控制器2种Get方法
public List<AlertModel> Get()
{
return _alertService.GetAllForUser(_loginService.GetUserID());
}
public AlertModel Get(int id)
{
return _alertService.GetByID(id);
}
然而,当我主动要求api/alerts
我得到以下错误:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'ekmSMS.Common.Models.AlertModel Get(Int32)' in 'ekmSMS.Web.Api.AlertsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
我在global.asax
设置了以下路线:
routes.MapHttpRoute("Api", "api/{controller}/{id}", new { id = UrlParameter.Optional });
如果th是超载工作的类型?如果它应该我做错了什么?
编辑
虽然这个问题是有关的WebAPI,控制器是一个MVC3项目的一部分,这些都是其他MapRoutes
:
routes.MapRoute("Templates", "templates/{folder}/{name}", new { controller = "templates", action = "index", folder = "", name = "" });
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "app", action = "index", id = UrlParameter.Optional });
我们可以看到所有的MapHttp路由?有时候可能会对他们产生一些干扰。 – 2012-08-03 11:33:53
@ user854301问题是关于Web API,而不是MVC。 – tugberk 2012-08-03 12:26:28
这是非常有趣的cuz路由参数的默认值是UrlParameter.Optional'被删除,如果它没有提供,所以动作选择逻辑应该在这里拿起Get()操作方法(对于任何请求来* API /警报*)。我会尝试重现错误。 – tugberk 2012-08-03 12:28:00