Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 具有齐次参数数组的MVC路由_Asp.net_Asp.net Mvc 2 - Fatal编程技术网

Asp.net 具有齐次参数数组的MVC路由

Asp.net 具有齐次参数数组的MVC路由,asp.net,asp.net-mvc-2,Asp.net,Asp.net Mvc 2,我正在尝试为具有一组同质参数的资源创建路由 URL如下所示: 产品/类别/{categoryId1}/{categoryId2}/../brand/{brandID1}/{brandID2}/ 并希望动作方法如下所示: 公共行动结果GetProducts(IList类别ID、IList品牌ID) {…} 其中类别和品牌是独立的过滤器 我找到了类似任务的解决方案: 不知道是否有更漂亮的解决方案可以使用这个原型 公共行动结果GetProducts(IList类别ID) 而不是 公共操作结果myAc

我正在尝试为具有一组同质参数的资源创建路由

URL如下所示: 产品/类别/{categoryId1}/{categoryId2}/../brand/{brandID1}/{brandID2}/

并希望动作方法如下所示: 公共行动结果GetProducts(IList类别ID、IList品牌ID) {…}

其中类别和品牌是独立的过滤器

我找到了类似任务的解决方案:

不知道是否有更漂亮的解决方案可以使用这个原型 公共行动结果GetProducts(IList类别ID)

而不是 公共操作结果myAction(字符串url)

行动法

--为了避免断线和浇铸

我怎样才能使这个解决方案适合我的情况呢


事先谢谢大家

使用自定义处理程序,就像我发布的那样

可能需要进行一些调整,但类似的操作应该可以:

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 ();

感谢您提供了一个很好的解决方案!我只是无法注册这样的路径:“产品/类别/{*categories}/brand/{*brands}”。发生运行时错误:catch all参数只能作为路由URLdo“products/category/{*parameters}”的最后一段出现,并使用.Split(“/brand/”)获取2个片段。。。然后它几乎已经在那里了:)。。。根据您的需要,您可能还希望在参数上添加一个约束,以便仅当/brand/在其中时才使用路由