1

美好时光!在输入 URL 浏览器中显示路线时出现问题。到网站上的搜索页面。搜索本身工作正常 - 传递了“密钥”,显示找到的列表。控制器中的搜索方法采用要搜索的字符串类型参数:

public ActionResult SearchAllByKey(string key)
        {
            //logic
            return View(<list_of_found>);
        }  

在 Global.asax 中规定的路线:

routes.MapRoute(
                  "Search",
                  "Search/{key}", 
                  new { controller = "controller_name", action = "SearchAllByKey", key = UrlParameter.Optional } 
              );

将 Edit 的值从 View 发送到方法的表单:

<% using (Html.BeginForm("SearchAllByKey", "controller_name", FormMethod.Post, new { enctype = "multipart/form-data" }))
                           {%>
                        <%: Html.ValidationSummary(true) %>
                        <input type="text" id="keyValue" name="key" />
                        <input type="submit" value="Go!" />
                        <% } %>

当您单击“开始!”时。到搜索结果页面,但 URL(输入行浏览器)显示:

http://localhost:PORT/Search

代替:

http://localhost:PORT/Search/SOME_KEY

如何确保在 URL-e 中可见“键”?提前致谢

4

1 回答 1

1

您正在发布您的数据。

更改您的 FORMFormMethod.Get并确保您的操作只接受 get (虽然这是默认设置)

    [HttpGet]
    public ActionResult SearchAllByKey(string key)
    {
        //logic
        return View(new List<string>());
    }  

更新

要实现您想要的,您必须在默认值之前配置您的 ruote:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
          "Search",
          "{controller}/SearchAllByKey/{key}",
          new { controller = "Home", action = "SearchAllByKey", key = UrlParameter.Optional }
      );

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

您的表格应如下所示:

<% using (Html.BeginForm("Search", "Home", FormMethod.Post))
{%>
    <% Html.ValidationSummary(true); %>
    <input type="text" id="key" name="key" value="" />
    <input type="submit" value="Go!" />
<% } %>

你必须像这样改变你的ActionResult

[HttpGet]
public ActionResult SearchAllByKey(string key)
{
    //logic
    return View(new List<string>());
}

[HttpPost]
public ActionResult Search(FormCollection form)
{
    return RedirectToAction("SearchAllByKey",  new { key = form["key"] });
}

基本上,您的FORM发布到Search重定向到SearchAllByKey.

于 2013-01-07T18:36:24.047 回答