简单的ASP.NET MVC路由问题

时间:2010-05-26 17:50:14

标签: asp.net-mvc asp.net-mvc-routing

我的简单MVC应用程序中有两个页面,其中有两个已定义的路径:

routes.MapRoute(
    "Results", // Route name
    "Results/{id}", // URL with parameters
    new { controller = "Results", action = "Index",
          id = "" } // Parameter defaults
);
routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Main", action = "Index",
          id = UrlParameter.Optional } // Parameter defaults
);

我需要仅使用产品ID加载结果页面,例如:[MyDomain ....] / Results / 12345。但是主页面使用这条路径对结果控制器进行POST(使用JQuery)进行更新:[MyDomain ....] / Main / Update以及数据包。当我只有“默认”路线时,这很好用。但是当我添加其他“结果”路由时,所有要更新的POST调用都失败了。什么想法我做错了???

非常感谢。

1 个答案:

答案 0 :(得分:0)

我没有尝试过,但应该完成你需要的东西。不确定是否有“更好”的方法来实现它。

routes.MapRoute(
  "Results", // Route name
  "Results/{id}", // URL with parameters
  new { controller = "Results", action = "Index", id = "" } // Parameter defaults
  new { id = @"\d+" } // regex for id param - id must be a number
);
routes.MapRoute(
  "Default", // Route name
  "{controller}/{action}/{id}", // URL with parameters
  new { controller = "Main", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
相关问题