代码之家  ›  专栏  ›  技术社区  ›  Fabio Milheiro

ASP.NetMVC路由不能正常工作

  •  2
  • Fabio Milheiro  · 技术社区  · 14 年前

    // Search (NOT working).
    routes.MapRoute(
             "Search",
             "search/{query}",
             new { controller = "Search", action = "Index" });
    
    // Homepage (I believe the problem could be here, but not sure).
    routes.MapRoute(
             "MainIndex",
             "{language}",
             new { controller = "Main", action = "Index", language = string.Empty });
    

    当我们在操作属性为“/search”的搜索表单中进行搜索时,用户被发送到主页,地址栏中的URL是“/search”?查询=示例+搜索”。

    <form id="form1" action="<%= Url.Action("Index", "Search") %>">
    

    在我看来是对的,但是操作名应该是“/search”而不是“/search”,对吗?

    3 回复  |  直到 14 年前
        1
  •  3
  •   Hector Correa    14 年前

    我刚试过你的路线

    <form id="form1"  method="post" action="<%= Url.Action("Index", "Search") %>">
    Enter something: <input type="text" name="query" id="query" value="hello" />
    <input type="submit" />
    </form>
    

    像这样的控制器

    public ActionResult Index(string query)
    {
        return View();
    } 
    

    而且效果不错。注意(1)我使用的是method=post和(2)textbox的名称和ID都设置为“query”,这是Html.TextBox会为你做的。这就是允许绑定获取值并将其正确传递给控制器的原因。

        2
  •  2
  •   dotariel    14 年前

    我总是发现这个工具对调试路由非常有用。 Route Debugger

        3
  •  1
  •   amurra    14 年前

    试着把 "search/{query}" "Search/{query}"

    你在表单标签上的动作是 /Search/Index ,与您的 Search/{query} 路由,但您的查询将是索引。但是 ?query=example+search 在路由的末尾,搜索路由将不知道如何处理该查询参数。我只需要更新form标记上的action属性 /Search