与同类参数数组MVC路线 [英] MVC route with array of homogeneous parameters

查看:113
本文介绍了与同类参数数组MVC路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图与同类参数的阵列的资源创建路线。

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

URL应该是这样的:
产品/分类/ {categoryId1} / {} categoryId2 /.../品牌/ {brandID1} / {} brandID2 / ...

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

和希望的操作方法是这样的:
公众的ActionResult的GetProducts(IList中的categoryID,ILIsts brandID)
{...}

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

在这里品类和品牌都是独立的过滤器。

where category and brand are independent filters.

我发现对于类似任务的解决方案:
<一href=\"http://stackoverflow.com/questions/3634582/asp-net-mvc-2-parameter-array\">http://stackoverflow.com/questions/3634582/asp-net-mvc-2-parameter-array

I found a solution for similiar task: http://stackoverflow.com/questions/3634582/asp-net-mvc-2-parameter-array

,不知道有没有更漂亮的解决方案,允许使用这个原型
公众的ActionResult的GetProducts(IList中的categoryID)

And wonder if there is no more beautiful solution that allow to use this prototype public ActionResult GetProducts(IList categoryID)

而不是
公众的ActionResult myAction(字符串URL)

instead of public ActionResult myAction(string url)

有关操作方法

- 为了避免分裂字符串和铸造?

-- to avoid splitting the string and casting?

和我怎么能适应这种解决方案我的情况?

And how could I suit this solution for my case?

感谢您事先大家!

推荐答案

使用自定义的处理器,像我张贴在<一个href=\"http://stackoverflow.com/questions/3758049/mvc-net-routing-routevalues-in-maproutes/3763562#3763562\">this回答。

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

这篇关于与同类参数数组MVC路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆