ASP.NET MVC 4一些路由不起作用

时间:2013-07-07 04:21:42

标签: c# asp.net-mvc-4

路线:

routes.MapRoute(
    "Customer_widget",
    "customer/widget/{action}/{id}",
    new { controller = "Customer_Widget", id = UrlParameter.Optional });

测试URL1 :(确实有效) customer/widget/contact_list/1-1004-SC-0-0-0-0-0-0-Supplier-Supplier----0-0-0-0-Year-Calendar-0-Home-0

测试网址2:(不起作用)

customer/widget/contact_list/1-1004-SC-0-0-0-0-0-0-Supplier-Supplier--6%2f1%2f2013-7%2f6%2f2013--0-0-0-0-Year-Calendar-0-Home-0  (does not work) 

我在上面测试了这两个网址。第一个URL到达正确的位置。但第二个网址却迷失了方向...... 我不知道是什么原因引起的...... 我有点假设白天的部分,6%2f1%2f2013-7%2f6%2f2013会导致一些问题,但我不确定那是什么。

CustomerController

 public ActionResult Index(string id = null)
    {
      string temp = "~/customer/widget/contact_list/" + this.objURL.ToString();
      return Redirect("~/customer/widget/contact_list/" + this.objURL.ToString());
    }

Customer_WidgetController

  public ActionResult Contact_list(string id = null)
    {
      return PartialView("_contact_list",Customer_Widget.Contact_list.Load(id, ref errors));
    }

flow CustomerController - >(通过map route)Customer_WidgetController

1 个答案:

答案 0 :(得分:0)

这都是因为编码的斜杠'%2f'符号对应于'/'。因此你的网址

customer/widget/contact_list/1-1004-SC-0-0-0-0-0-0-Supplier-Supplier--6%2f1%2f2013-7%2f6%2f2013--0-0-0-0-Year-Calendar-0-Home-0

分为8个部分:

  1. 客户
  2. 插件
  3. CONTACT_LIST
  4. 1-1004-SC-0-0-0-0-0-0-供应商-供应商 - 6
  5. 1
  6. 2013-7
  7. 6
  8. 2013--0-0-0-0年日历-0 - 首页 - 0
  9. 但在你的路线中,你期待4。

    为了定义段的变量计数,您可以使用星号(*),如下所示:

    routes.MapRoute(
        "Customer_widget",
        "customer/widget/{action}/{*id}",
        new { controller = "Customer_Widget", id = UrlParameter.Optional });
    

    路由系统按顺序检查路由。因此,您需要小心这一点,并尽可能低地定义这样的路线,因为它可以捕获您不希望捕获此路线的请求。例如,如果在上面的路由之后系统中将定义以下路由,则永远不会捕获它:

    routes.MapRoute(
            "Customer_widget",
            "customer/widget/{action}/{lang}/{*id}",
            new { controller = "Customer_Widget", lang = "en", id = UrlParameter.Optional }
            new { lang = "en|es|ru"});
    
相关问题