【问题标题】:Routing Issue on asp.net mvc 5 GETasp.net mvc 5 GET 上的路由问题
【发布时间】:2014-02-05 15:00:01
【问题描述】:

我试图让我的产品搜索 URL 看起来像“产品/搜索/{此处搜索词}”。

我正在使用基于属性的路由,我的控制器操作如下所示:

[HttpGet]
[Route("Products/Search/{searchTerm?}", Name="ProductSearch")]
public ActionResult Search(string searchTerm = "")
{
    return View();
}

我曾尝试使用 HTML Helper for BeginForm 和 BeginRouteForm(如下所示),但都没有成功。正在调用正确的操作,但我的 URL 看起来像“Products/Search?searchTerm”

BeginRouteForm

    @using (Html.BeginRouteForm("ProductSearch", new { searchTerm = "" }, FormMethod.Get, new { Class = "navbar-form navbar-right", role = "search" }))
    {
        <div class="form-group">
            @Html.TextBox("searchTerm", null, new { Class = "form-control", placeholder = "Item # or Name" })
        </div>
        <button type="submit" class="btn btn-default">Search</button>
    }

BeginForm

        @using (Html.BeginForm("Search", "Products", new { searchTerm = "" }, FormMethod.Get, new { Class = "navbar-form navbar-right", role = "search" }))
        {
            <div class="form-group">
                @Html.TextBox("searchTerm", null, new { Class = "form-control", placeholder = "Item # or Name" })
            </div>
            <button type="submit" class="btn btn-default">Search</button>
        }

我已经通过调试并选择了正确的路线,但 URL 没有显示我想要的样子。我错过了什么?

【问题讨论】:

  • 您看到的此请求的网址是什么?
  • 我看到的是 Products/Search?searchTerm=text
  • 你需要Search(string searchTerm = "")默认值吗?尝试作为 Search(string searchTerm) 离开,因为它是 Route 中指定的可为空类型
  • 感谢您的建议。我只是试了一下,结果还是一样。

标签: c# asp.net-mvc asp.net-mvc-routing asp.net-mvc-5


【解决方案1】:

这是我建议的解决方案 -

你有以下控制器动作 -

    [HttpGet]
    public ActionResult Search(string searchTerm = "")
    {
        return View();
    }

让视图成为 -

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>

    $(function () {
        $('#click').click(function (e) {

            var name = $("#search").val();
            var url = '@Url.Action("Search", "Action")' + '/' + name;
            window.location.href = url;

        });
    });

</script>

<input type="text" name="searchText" id="search"/>
<input type="button" value="click" id="click"/>

当你点击按钮时——

不要忘记将正确的路由添加到路由配置中 -

routes.MapRoute(
       name: "searchaction",
       url: "{controller}/{action}/{searchTerm}",
       defaults: new { controller = "Action", action = "Search" }
    );

【讨论】:

    【解决方案2】:

    您认为自己遇到的问题并不是因为 ASP.Net MVC 的任何原因。所有使用 GET 方法的 Html 表单都会将所有输入元素转换为 QueryString 参数。这只是一个W3C standard

    如果你想让这个工作,你必须编写 jQuery 来在表单提交之前抛出一个事件,从输入中获取文本值临时存储它,清空输入框,然后通过追加更新动作临时值。

    【讨论】:

      【解决方案3】:

      我认为 BeginRouteForm 的工作方式与您期望的不同。根据documentation,该方法所做的只是使用提供的参数插入&lt;form&gt;。如果您为路由值提供了空字符串以外的其他内容,例如 new { searchTerm = "somesearchterm" },您会在 Url 中看到它显示为“/product/search/somesearchterm”。然而,就像现在一样,表单将正常处理,将搜索词放在 Url 上作为正常查询参数。

      【讨论】:

      • 我尝试了 new { searchTerm = "test" },现在当我提交表单时,我得到了 URL: Products/Search/test?searchTerm=[input value here] 。我还缺少其他东西来获取产品/搜索/[此处输入值]?
      • 是的。该表单不会自动将搜索词附加到目标 URL。该 Url 是在运行时生成的,除非使用 JavaScript 进行更改,否则不会更改。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 2011-11-30
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多