C# 如何将多个DateTime参数传递给ApicController方法?
(我正在使用WebApi 4.0.20710.0和framework 4.0。我不确定这是否重要,但我们还没有被允许将服务器升级到4.5) 我想做的是打这样的电话:C# 如何将多个DateTime参数传递给ApicController方法?,c#,asp.net-mvc-4,asp.net-web-api,asp.net-mvc-routing,C#,Asp.net Mvc 4,Asp.net Web Api,Asp.net Mvc Routing,(我正在使用WebApi 4.0.20710.0和framework 4.0。我不确定这是否重要,但我们还没有被允许将服务器升级到4.5) 我想做的是打这样的电话: http://domain/api/Books/Outstanding/2013-01-01/2014-01-01 /// <summary> /// Get a list of books that were outstanding between the start and end dates. /// </
http://domain/api/Books/Outstanding/2013-01-01/2014-01-01
/// <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);
}
routes.MapRoute(
name: "ActionWithStartAndEnd",
url: "{controller}/{action}/{start}/{end}"
);
在我的ApiController
中,我有一个类似这样的方法:
http://domain/api/Books/Outstanding/2013-01-01/2014-01-01
/// <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);
}
routes.MapRoute(
name: "ActionWithStartAndEnd",
url: "{controller}/{action}/{start}/{end}"
);
然而,这一切实际上都不起作用。最后,我看到一条错误消息,上面简单地说,“在控制器‘Books’上找不到与请求匹配的操作。”
我怀疑我做了一些简单的错误的事情,和/或误解了一些关于路由的一般性的事情。我该怎么做?我是个白痴
路由不属于RouteConfig,它属于WebApiConfig,它应采用以下形式:
config.Routes.MapHttpRoute(
name: "ActionWithStartAndEnd",
routeTemplate: "api/{controller}/{action}/{start}/{end}"
);
但是你的动作名称是
getsumment
而不是summent
我不得不将代码匿名,可能很糟糕。坦率地说,违约是一种误导。我会移除它们。感谢你回来并给出一个清晰的答案!别忘了你可以接受自己的答案。谢谢!如果允许的话,我会在几天内接受答案(除非其他人设法插嘴提出更雄辩的批评;)