jcv*_*dan 4 .net c# asp.net-web-api
我有webapi控制器,有2个动作方法,如下所示:
public List<AlertModel> Get()
{
return _alertService.GetAllForUser(_loginService.GetUserID());
}
public AlertModel Get(int id)
{
return _alertService.GetByID(id);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我发出请求时,api/alerts我收到以下错误:
参数字典包含'ekmSMS.Web.Api.AlertsController'中方法'ekmSMS.Common.Models.AlertModel Get(Int32)'的非可空类型'System.Int32'的参数'id'的空条目.可选参数必须是引用类型,可空类型,或者声明为可选参数.
我有以下路线设置global.asax:
routes.MapHttpRoute("Api", "api/{controller}/{id}", new { id = UrlParameter.Optional });
Run Code Online (Sandbox Code Playgroud)
这种类型的超载是否有效?如果它应该是我做错了什么?
编辑
虽然这个问题与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 });
Run Code Online (Sandbox Code Playgroud)
tug*_*erk 12
问题是您使用UrlParameter.Optional(这是一种ASP.NET MVC特定类型)而不是RouteParameter.Optional.如下更改您的路线然后它应该工作:
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
"Api",
"api/{controller}/{id}",
new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3995 次 |
| 最近记录: |