Say I will be passing below URL requests to the routing system, this is the longest URL possible.
store/storecontent/pets/cat/food/1
Where “1” is the page number and pets is the rootpointer
Now my route entry looks like so:
routes.MapRoute(
"StoreContent", // Route name
"{controller}/{action}/{rootpointer}/{category}/{category2}/{page}", // URL with parameters
new { rootpointer = UrlParameter.Optional, category= UrlParameter.Optional, category2 = UrlParameter.Optional, page = UrlParameter.Optional},
new { controller = "Store", action = "StoreContent" });
My method like so:
public PartialViewResult StoreContent(string rootpointer, string category, string category2, int page) {}
The problem is when I pass a URL like store/storecontent/pets/1 then category gets assigned the “page” value, understandably as page is now positioned in the place of the route segment reserved for category and routing system has no way of telling which value belongs to page and which to category since these are anonymous types.
Is there any way around this, I understand this can be done manually by using catchall and parsing it to extract values, but is there a more graceful solution that does not need any manual intervention ? I could also probably just use something like /page-{page}/ if my URL’s reflect it then routing system should pick-up it’s for the page anonymous type and assign its value correctly to the page parameter on the method?
This is not the way that routing works
If you define a route as
And then access your page using store/storecontent/pets/1 then your route parameters are as follows:
What you’re trying to do is actually store/storecontent/pets///1 but you can’t provide such URLs
RESTful?
This is also not RESTful. Why? Because providing page as part of your URL returns arguably nondeterministic results…
That’s why paging and sorting should be (in terms of REST) provided as query variables as in store/storecontent/pets?page=1. This would of course work. They become arbitrary variables not related to routing per se. Try if this also works for your route definition because it may as well work, since many of your route segments are optional.
Possible solution 1
The possible solution (although I would suggest you keep paging as query variable) would be to define several routes:
each of these would need constraints on
page = "\d+"so routing follows along until correct one gets hit.Possible solution 2
By keeping page as a separate query parameter nobody said you can’t define it as part of the defaults:
As you can see I’ve provided a default value for page. I know you’ve set it as optional, but all I’m saying here is that you can provide defaults for non-route related values. Query variables for example.
Additional observation
If you provide single value constant constraints for controller and action there’s no need to keep them in route definition as route values the way that you did. Your current route as it’s defined at the moment would be better of as: