Web.Api在路由中定义可选参数

时间:2014-11-06 14:43:17

标签: asp.net-web-api routes optional

在控制器端点的路由上定义可选参数的正确语法是什么?

    [Route("customers/{customerId}/boats/{boatId}/bookings{contractId?}&{startDate?}&{endDate?}&{outOnly?}", Name = "GetBookingsByBoat")]
    [ResponseType(typeof (IEnumerable<BookingSummaryDTO>))]
    public IHttpActionResult Get(int customerId, int boatId, int? contractId, DateTime? startDate, DateTime? endDate, bool outOnly = false)
    {

以上编译,但是当我尝试使用

调用端点时
customers/40/boats/24/bookings

我明白了:

{"message":"The requested resource does not support http method 'GET'."}
非常感谢

1 个答案:

答案 0 :(得分:0)

这是解决方案:

    [Route("customers/{customerId}/boats/{boatId}/bookings", Name = "GetBookingsByBoat")]
    [ResponseType(typeof (IEnumerable<BookingSummaryDTO>))]
    public IHttpActionResult Get(int customerId, int boatId, int? contractId = null, DateTime? startDate = null, DateTime? endDate = null, bool outOnly = false)