【问题标题】:ASP.net web api 2 error "The parameters dictionary contains a null entry for parameter"ASP.net web api 2 错误“参数字典包含参数的空条目”
【发布时间】:2014-05-19 12:31:12
【问题描述】:

我的路由配置如下:

   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);

    }
}

但是我收到了这个错误:

{"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."}

如果我将方法 SingleRoutine 更改为:

    [HttpGet]
    [ActionName("SingleRoutine")]
    // GET api/routine/5
    public RoutineViewModel SingleRoutine(int? id)
    {
        if (!id.HasValue)
        {
            return null;
        }
        return _routineService.GetRoutineById((int) id);
}

在浏览器中我只看到“null”。

为什么会这样?

编辑

当我输入 /api/routine/tags 时显示此错误

【问题讨论】:

  • 错误出现在哪一行?我敢打赌它不在这里。
  • 不抛出异常,浏览器显示此错误
  • 我不知道这是否重要,但我使用 Niject 作为 IoC
  • 你调用控制器的 url 是什么?试试“api/routine/SingleRoutine/5”。
  • 产生此错误的请求是什么?该请求中的 id 值在哪里?

标签: c# asp.net asp.net-web-api asp.net-web-api2


【解决方案1】:

我相信这是 WebApiConfig 中的问题我有以下几行:

config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new {id = RouteParameter.Optional}
            );

        config.Routes.MapHttpRoute("CustomApi", "{controller}/{action}/{id}", new {id = RouteParameter.Optional}
            );

在路由配置中

 routes.MapHttpRoute("DefaultApi2", "api/{controller}/{action}/{id}", new {id = RouteParameter.Optional}

现在在 WebApiConfig 我有:

 config.Routes.MapHttpRoute("CustomApi", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional }
         );
        config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new {id = RouteParameter.Optional}
            );

在 RouteConfig 中:

  routes.MapRoute(
             name: "Default",
             url: "{controller}/{action}/{id}",
             defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
         );

它有效!

【讨论】:

  • 请在更新中包含您的问题。您应该在问题中提供所有这些信息。
  • @Guanxi 但这是解决方案,现在可以了,我为什么要把它放在 Question 中?
【解决方案2】:

for api/{controller}/{action}/{id} route URL api/routine/5 搜索不存在的函数(action)5

更新:

您的错误消息有它失败的原因:可选参数必须是引用类型、可空类型或声明为可选参数。"}

int 不可为空,因此当您不提供其值时,它无法创建 id。

【讨论】:

  • 我已经编辑了我的问题,当我尝试去路由 api/routine/tags 时出现问题
  • 但是当我将其更改为 int 时? null 在浏览器中显示,这意味着调用 SingleRoutine 而不是标签?
  • 您是否调试并检查过调用了哪个函数。 _routineService.GetAllTags() 也可以返回 null。
  • 是的,我有,但调用 SingleRoutine 的情况并非如此
  • DefaultApi2 --- 你有配置其他路由吗?在 WebApiconfig 或 RouteConfig 中
【解决方案3】:

使用基于约定的路由(安装 nuget 包)

在您的 web api 上添加定义的以下类型路由

[Route("api/routine/{id}")]

【讨论】:

    猜你喜欢
    • 2016-10-14
    • 1970-01-01
    • 2013-12-15
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多