Asp.net mvc 为什么这条路线不正确

Asp.net mvc 为什么这条路线不正确,asp.net-mvc,routes,Asp.net Mvc,Routes,我的MVC4网站显示了数据库中的项目,用户可以从web表单中“优化”他们的搜索。之后,他们单击搜索按钮并显示结果 在这个阶段,我只有一条路线 routes.MapRoute( name: "Default", url: "{controller}/{action}/", defaults: new { controller = "Home", action = "Index" } ); 当我第一次加载页面时,地址是www.mydo

我的MVC4网站显示了数据库中的项目,用户可以从web表单中“优化”他们的搜索。之后,他们单击搜索按钮并显示结果

在这个阶段,我只有一条路线

 routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/",
          defaults: new { controller = "Home", action = "Index" }
 );
当我第一次加载页面时,地址是
www.mydomain.com/products/connectors/
,在我进行搜索后,它会附加我的查询字符串

www.mydomain.com/products/connectors/?Gender=1

现在,我正在添加分页,希望用户能够选择下一页。我用马克的答案来做这件事。分页非常有效

这是routeconfig.cs

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "AllRefineSearch",
            url: "{controller}/{action}/{startIndex}/",
            defaults: new { controller = "Products", action = "Connectors", startIndex = 0, pageSize = 10 }
        );

        routes.MapRoute(
           name: "Default",
           url: "{controller}/{action}/",
           defaults: new { controller = "Home", action = "Index" }
       );
    }
}
但现在的问题是,当我单击搜索按钮时,我的控制器和操作将从地址中删除。换句话说,地址是
www.mydomain.com/?Gender=1

我不知道如何在URL中保留控制器和操作,因为我认为路由指定了这个

我的表格是

    @using (Html.BeginForm("Connectors", "Products", FormMethod.Get))
    {

      @Html.DropDownListFor(a => a.Gender, Model.ConnectorRefineSearch.Gender, "---Select---")  
      <input type="submit" value="Search" class="searchButton" />
    }

问题在于您的AllRefineSearch路由及其默认值:“产品”控制器、“连接器”操作和“0”startIndex

在管线中提供默认值也意味着这些管段是可选的。在您的例子中,url“/”将匹配该路由,并且每个段将采用其默认值

在为链接、表单等生成Url时,将遵循类似的过程。您提供“产品”作为控制器,“连接器”作为操作,因此将使用AllRefineSearch。因为它们是默认值,所以它会将表单标记的url简化为“/”。最后,在提交时,input值被添加到查询字符串中

尝试在控制器和动作段的搜索路径中不提供默认值:

routes.MapRoute(
    name: "AllRefineSearch",
    url: "{controller}/{action}/{startIndex}/",
    defaults: new { startIndex = 0, pageSize = 10 }
);

什么是“搜索按钮”?它有什么作用?@David,我已经为你更新了我的问题(显示表格的部分)
routes.MapRoute(
    name: "AllRefineSearch",
    url: "{controller}/{action}/{startIndex}/",
    defaults: new { startIndex = 0, pageSize = 10 }
);