加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 06:54:03 所属栏目:asp.Net 来源:网络整理
导读:我尝试使用一组齐次参数为资源创建路由. 网址如下所示: 产品/分类/ {categoryId1} / {} categoryId2 /…/品牌/ {brandID1} / {} brandID2 / … 并希望一个动作方法看起来像这样: public ActionResult GetProducts(IList categoryID,ILIsts brandID) {…}
我尝试使用一组齐次参数为资源创建路由.

网址如下所示:
产品/分类/ {categoryId1} / {} categoryId2 /…/品牌/ {brandID1} / {} brandID2 / …

并希望一个动作方法看起来像这样:
public ActionResult GetProducts(IList categoryID,ILIsts brandID)
{…}

类别和品牌是独立过滤器.

我找到了类似任务的解决方案:
ASP.NET MVC 2 Parameter Array

并且想知道是否有更美丽的解决方案允许使用这个原型
public ActionResult GetProducts(IList categoryID)

代替
public ActionResult myAction(string url)

行动方法

– 避免分裂弦和铸造?

我怎么能为我的案子适合这个解决方案?

先谢谢大家!

解决方法

使用自定义处理程序,就像我在 this answer中发布的那样.

可能需要一些调整,但这样的事情应该有效:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读