I am using ASP.NET MVC 4 Beta with Web API - although I believe the routing implementation has no significant differences from .NET 4.0 relevant to this question. MapHttpRoute, used in the code below, ships with MVC 4 and is an extension method that essentially performs the same function as MapRoute, but for WebApi controllers.
I have a legacy web serivce method for which I want create an ASP.NET route. The method currently has following signature:
NewsItemList ListNewsItems(string category, int maxResults)
It should be mapped by requests whose URIs look like this:
~/news?category=finance&maxResults=50
Neither, either or both query string parameters can be present.
I am struggling to find a way to define a route to support this method.
I used MapHttpRoute to register the route and set defaults for the controller and action values to the controller and method appropriately. Here are some templates I have tried with the outcome:
"news"captures"~/news"and binds to the method correctly, but won't capture anything with a querystring."news{*query}is not allowed because you cannot have a catch-all unless it is exclusive to the Uri segment"news{query}"captures the query string but doesn't bind to the method
I'm looking for a way forwards that isn't too radical a departure as this is an edge-case in the application. The solution has to fit in with many other existing routes. Ideally I'd like to avoid going down the Uri re-writer path as well.