【问题标题】:Simple ASP.NET MVC Routing question简单的 ASP.NET MVC 路由问题
【发布时间】:2010-05-27 01:58:35
【问题描述】:

我的简单 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 调用都失败了。任何想法我做错了什么???

非常感谢。

【问题讨论】:

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


    【解决方案1】:

    我没有尝试过,但应该可以满足您的需求。不确定是否有“更好”的方法来完成它。

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

    【讨论】:

      猜你喜欢
      • 2011-03-19
      • 2011-01-09
      • 1970-01-01
      • 2013-11-13
      • 2011-11-30
      • 2013-06-07
      • 1970-01-01
      相关资源
      最近更新 更多