MVC route with array of homogeneous parameters

前端 未结 1 1676
清酒与你
清酒与你 2021-01-01 03:05

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

URL would look like this: products/category/{categoryId1}/{categoryId2}/.../brand

相关标签:
1条回答
  • 2021-01-01 04:00

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

    Might need some adjustments, 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 ();
    
    0 讨论(0)
提交回复
热议问题