【问题标题】:asp mvc routing with two optional parameters带有两个可选参数的asp mvc路由
【发布时间】:2011-06-30 23:32:58
【问题描述】:

嗨,当两个参数都是可选的时,我如何映射 url ../Companies/Results/value/id?

Companies 是控制器,Results 是操作,value 和 id 是可选参数。在我的表单上是一个值的文本框和一个 id 的选择列表。用户可以选择两者或其中之一进行搜索。尝试过这样的事情,但是当缺少可选参数之一(例如值)时无法处理,例如 ../Companies/Results/ /id

        routes.MapRoute(
            "Company+Profession", // Route name
            "{action}/{value}/{profId}", // URL with parameters
            new { controller = "Companies", action = "Index", value = UrlParameter.Optional, profId = UrlParameter.Optional } // Parameter defaults
        );

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

【问题讨论】:

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


    【解决方案1】:

    您不能拥有具有两个可选参数的路由,由于您描述的问题,只有最后一个参数可以是可选的。我建议您为value 设置一个默认参数,例如byid,并在人们选择职业时使用它。

    我假设您是通过 javascript 构建 URL,因为使用 GET 表单操作会导致将参数名称添加到 URL。在这种情况下,当文本框为空时,只需插入默认的 byid。

    更新您的路由以包含默认值,以便您生成的任何 URL 都可以使用。请参阅 Phil Haack 的 blog post 以了解另一种处理生成具有两个“可选”参数的 URL 的方法。

    // used when both parameters are specified
    routes.MapRoute(
            "Company+Profession", // Route name
            "{action}/{value}/{profId}", // URL with parameters
            new { controller = "Companies", action = "Index", value ="byid", profId = UrlParameter.Optional } // Parameter defaults
    );
    

    【讨论】:

      【解决方案2】:

      谢谢大家,刚刚发现整数的路由限制。如此摆弄一些路线组合,它似乎按我想要的方式工作:

              routes.MapRoute(
                 "Detail", // Route name
                 "{action}/{value}", // URL with parameters
                 new { controller = "Companies", action = "Detail" }, // Parameter defaults
                 new { value = @"\d+" } //integer only
              );
      
              routes.MapRoute(
                  "Company + Profession", // Route name
                  "{action}/{value}/{profId}", // URL with parameters
                  new { controller = "Companies", action = "Results" }, // Parameter defaults
                  new { profId = @"\d+" } //integer only
              );
      
              routes.MapRoute(
                  "Profession", // Route name
                  "{action}/{profId}", // URL with parameters
                  new { controller = "Companies", action = "Results"}, // Parameter defaults
                  new {profId = @"\d+" } //integer only
              );
      
              routes.MapRoute(
                 "Company", // Route name
                 "{action}/{value}", // URL with parameters
                 new { controller = "Companies", action = "Results" } // Parameter defaults
             );
      
              routes.MapRoute(
                  "RootFolder", // Route name
                  "{action}/{value}", // URL with parameters
                  new { controller = "Companies", action = "Index", value = UrlParameter.Optional } // Parameter defaults
              );
      

      【讨论】:

        【解决方案3】:

        我不确定,因为我现在没有在哪里尝试这个,但这是我的建议

        routes.MapRoute(
                    "Company+Profession", // Route name
                    "Companies/{action}/value-{value}/id-{profId}", // URL with parameters
                    new { controller = "Companies", action = "Index", value = UrlParameter.Optional, profId = UrlParameter.Optional } // Parameter defaults
                );
        

        【讨论】:

          猜你喜欢
          • 2014-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-24
          • 2013-05-07
          • 2011-04-08
          • 1970-01-01
          相关资源
          最近更新 更多