MVC route with an array of homogeneous parameters

I am trying to create routes for a resource with an array of homogeneous parameters.

The URL will look like this: products / categories / {categoryId1} / {categoryId2} /.../ brand / {brandID1} / {brandID2} / ...

And I would like the action method to look like this: public ActionResult GetProducts (IList categoryID, ILIsts brandID) {...}

where category and brand are independent filters.

I found a solution for a similar problem: ASP.NET MVC 2 parameter array

And I wonder if there is a more beautiful solution to use this prototype public ActionResult GetProducts (IList categoryID)

instead of public ActionResult myAction (string url)

for action method

- to avoid line separation and casting?

And how can I satisfy this solution for my case?

Thanks to everyone in advance!

+4
source share
1 answer

Use a special handler like the one I posted in this answer .

Some adjustments may be required, but something like this should work:

public class ProductsRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { IRouteHandler handler = new MvcRouteHandler(); var vals = requestContext.RouteData.Values; vals["categoryID"] = vals["categories"].Split("/"); vals["brandID"] = vals["brands"].Split("/"); return handler.GetHttpHandler(requestContext); } } // in the route: routes.MapRoute( "test", "products/category/{*categories}/brand/{*brands}", new { Controller = "product", Action = "getproducts"} ).RouteHandler = new ProductsRouteHandler (); 
+7
source

All Articles