【问题标题】:ASP.NET MVC3 Custom Route Mapping?ASP.NET MVC3 自定义路由映射?
【发布时间】:2012-04-28 02:49:03
【问题描述】:

网址如:

/query/test/p1s1c1

动作是:

public ActionResult Test(int price = 1, int size = 1, int category = 1)
{
    ViewBag.param = "price:" + price + "size:" + size + "category" + category;
    return View();
}

我的路线图是:

routes.MapRoute(
    "Query", // Route name
    "Query/test/p{price}s{size}c{category}",
    new { controller = "Query", action = "Test", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
    new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
    );

但它不起作用,谁能帮助我?

【问题讨论】:

    标签: asp.net-mvc-3 routes action


    【解决方案1】:

    当框架处理 URL 请求时,它会尝试将请求的 URL 与 Routes 添加到 RouteCollection 的顺序相匹配。

    因此,将您的路线放在默认路线之前,它应该可以工作:

    routes.MapRoute(
                    "Query", // Route name
                    ///...
                    );
    
    routes.MapRoute(
                    "Default", // Route name
                    ///...
                   );
    

    现在带有 url :/query/test/p2s2c2 的执行测试操作将具有参数:price = 2, size = 2, category = 2

    您可以阅读有关How URLs Are Matched to Routes 的更多信息。

    【讨论】:

      【解决方案2】:

      试试这个...

      routes.MapRoute(
          "Query", // Route name
          "Query/test/{price}/{size}/{category}",
          new { controller = "Query", action = "Test", price = UrlParameter.Optional, size = UrlParameter.Optional, category = UrlParameter.Optional },
          new { price = @"\d+", size = @"\d*" , category = @"\d*" } // Parameter defaults 
          );
      

      在默认路由之后......

      【讨论】:

        猜你喜欢
        • 2015-05-03
        • 1970-01-01
        • 2012-02-18
        • 1970-01-01
        • 1970-01-01
        • 2011-10-11
        • 1970-01-01
        • 2010-09-05
        • 1970-01-01
        相关资源
        最近更新 更多