【问题标题】:How should I pass multiple DateTime parameters to an ApiController method?我应该如何将多个 DateTime 参数传递给 ApiController 方法?
【发布时间】:2014-02-10 20:02:41
【问题描述】:

(我正在使用 WebApi 4.0.20710.0,框架 4.0。我不确定这是否重要,但我们还没有被允许将服务器升级到 4.5)

我想做的是这样打电话:

http://domain/api/Books/Outstanding/2013-01-01/2014-01-01

在我的ApiController 中,我有一个类似这样的方法:

/// <summary>
/// Get a list of books that were outstanding between the start and end dates.
/// </summary>
public IEnumerable<BookDto> GetOutstanding(DateTime start, DateTime end)
{
    return _repository.GetOutstanding(start, end);
}

在我的RouteConfig 中,我有一个这样定义的路线:

routes.MapRoute(
    name: "ActionWithStartAndEnd",
    url: "{controller}/{action}/{start}/{end}"
);

然而,这些都没有真正奏效。我最终得到一条错误消息,上面写着"No action was found on the controller 'Books' that matches the request."

我怀疑我做错了一些简单的事情,和/或对一般路由有误解。我该怎么做?

【问题讨论】:

  • 但是您的操作名称是 GetOutstanding 而不是 OutStanding
  • 我不得不匿名化代码,可能很糟糕。坦率地说,默认值具有误导性。我会删除它们。

标签: c# asp.net-mvc-4 asp.net-web-api asp.net-mvc-routing


【解决方案1】:

我是个白痴。

路由不属于RouteConfig,属于WebApiConfig,应该采用这种形式:

config.Routes.MapHttpRoute(
    name: "ActionWithStartAndEnd",
    routeTemplate: "api/{controller}/{action}/{start}/{end}"
);

【讨论】:

  • +1 回来并给出明确的答案!不要忘记您可以接受自己的答案。
  • 谢谢!几天后,我会在被允许的情况下接受答案(除非其他人设法以更雄辩的弃权方式介入;)
猜你喜欢
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
相关资源
最近更新 更多