adhocgeek
adhocgeek

Reputation: 1457

How should I pass multiple DateTime parameters to an ApiController method?

(I'm using WebApi 4.0.20710.0, with framework 4.0. I'm not sure if this is important, but we haven't been allowed to upgrade our servers to 4.5 yet)

What I want to do is make a call like this :

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

In my ApiController I have a method something like this :

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

And in my RouteConfig I have a route defined like this :

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

Yet none of this actually works. I end up with an error message that says simply "No action was found on the controller 'Books' that matches the request."

I suspect I'm doing something simple incorrectly, and/or have misunderstood something about routing in general. How should I be doing this?

Upvotes: 0

Views: 1475

Answers (1)

adhocgeek
adhocgeek

Reputation: 1457

I'm an idiot.

The route doesn't belong in RouteConfig, it belongs in WebApiConfig, and it should take this form :

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

Upvotes: 1

Related Questions