【问题标题】:Redirect Loop MVC3 Search Form Using HttpGet使用 HttpGet 重定向循环 MVC3 搜索表单
【发布时间】:2012-01-12 08:22:15
【问题描述】:

希望有人能提供帮助,因为昨天晚上的大部分时间我都在绞尽脑汁!基本上,我正在尝试使用 HttpGet 使搜索表单工作,因此我可以通过以下 URL 从外部源检索结果:

http://url.com/Area/Controller/Action/SearchCategory/SearchCriteria

我创建一个模型并将其传递给我的视图,其中包含 SearchCategorySearchCriteria 的两个属性,并在视图中具有相关的 HTML 控件。如果我同时选择一个类别并为我的标准输入一些内容,这将非常有效。但是,如果我没有在我的标准中输入任何内容,我会得到一个无限重定向。我的这个特定功能的路线如下所示:

context.MapRoute(
    "Dashboard-Search",
    "Area/Controller/Action/{SearchCategory}/{SearchCriteria}",
    new { 
        controller = "Controller", 
        action = "Action", 
        SearchCategory = "", 
        SearchCriteria = "" 
    }
);

我确实让我的模型实现了 IValidateableObject 并验证是否输入了某些内容,但显然路由绑定是在验证任何内容之前完成的。

有什么想法吗???

路线

context.MapRoute(
    "Dashboard-Search-NoCriteria",
    "HEP/Dashboard/Search/{category}",
    new { controller = "Dashboard", action = "Search", category = "Case No" }
);

context.MapRoute(
    "Dashboard-Search",
    "HEP/Dashboard/Search/{category}/{criteria}",
    new { controller = "Dashboard", action = "Search", category = "Case No", criteria = "" }
);

控制器动作

[HttpGet]
public ActionResult Search(string SearchCategory, string SearchCriteria)
{
    // create new instance of model and add search criteria so entered
    // data persists on post back
    DashboardModel model = new DashboardModel() {
        SearchCategory = SearchCategory,
        SearchCriteria = SearchCriteria
    };
    model.Search(SearchCategory, SearchCriteria);

    // return the HTML view of another controller that displays the same list,
    // only this time, the list is filtered according to GET data
    return View("Overview", model);
}

HTML 表单

@using (Html.BeginForm("Search", "Dashboard", FormMethod.Get)) {

    @Html.LabelFor(m => m.SearchCategory, "Category:")
    @Html.DropDownListFor(m => m.SearchCategory, new List<SelectListItem>() {
        new SelectListItem() { Selected = true, Text = "Category", Value = "Category" }
    })

    @Html.LabelFor(m => m.SearchCriteria, "Criteria:")
    @Html.TextBoxFor(m => m.SearchCriteria)

    <input type="submit" value="Search" class="button" />

}

【问题讨论】:

  • 我知道我可以通过使用客户端验证来防止这种情况发生由于某种原因被禁用。我认为无限循环看起来就像我做的不对!
  • “不要在我的条件中输入任何内容”是什么意思? Criteria 是作为 GET 参数还是作为 URL 部分传递(您的路由代码看起来像后一个选项)?如果条件作为 URL 的一部分传递,则不指定它意味着您的 URL 与 Dashboard-Search 路由的模式不匹配,并且它正在通过其他路由重定向。您能否提供更多关于您如何传递标准的信息(示例 URL?)并发布您的整个 RegisterRoutes 函数代码?
  • 中的表单上有字面上的 2 个输入框,当提交时(带有条件中的某些内容),键/值对被放入查询字符串 OK。但是,如果我不输入任何条件,则只会将类别的键/值对附加到查询字符串中。除非在标准输入中特别输入了某些内容,否则我基本上要以某种方式阻止路由。
  • 如果您使用的是form method="get",那么您的action 是什么?如果它为空(未指定),则它必须是当前 URL - 您的搜索表单可从中访问的 URL。可以发一下吗?
  • 我开始认为也许我应该在控制器的操作中发布数据并在那里处理存在检查。如果已发布标准,则 RedirectToAction 使用正确的 URL 参数,否则将验证错误返回到视图?

标签: c# asp.net-mvc-3 routes http-get


【解决方案1】:

您目前的路线与您的搜索表单不对应。

满足您的Dashboard-Search 路由的网址应如下所示

http://example.com/HEP/Dashboard/Search/SampleCategory/SampleCriteria

您的表单反过来会产生一个对 URL 的 GET 请求,如下所示:

http://example.com/HEP/Dashboard/Search/?Category=SampleCategory&Criteria=SampleCriteria

很可能从未调用过您的方法 Search(string SearchCategory, string SearchCriteria) - 我建议将您的请求重定向回仅显示您的搜索视图而不实际执行任何搜索的方法。

如果您需要更多解释或代码,请显示整个 RegisterRoutes 方法。

【讨论】:

    【解决方案2】:
    [HttpGet]
    public ActionResult Search(string SearchCategory = null, string SearchCriteria = null)
    

    这将允许您的控制器在不使用 searchcategory 或 searchcriteria 的情况下工作,只要您处理它们为空的情况。

    【讨论】:

      【解决方案3】:

      我建议使用单一路由,然后通过将标准设为可选来以不同方式设置默认值。

      context.MapRoute(
          "Dashboard-Search",
          "HEP/Dashboard/Search/{category}/{criteria}",
          new { controller = "Dashboard", action = "Search", category = "Case No", criteria = UrlParameter.Optional });
      

      【讨论】:

        猜你喜欢
        • 2013-07-10
        • 1970-01-01
        • 1970-01-01
        • 2011-01-15
        • 2013-07-22
        • 1970-01-01
        • 2021-05-21
        • 2014-01-07
        • 1970-01-01
        相关资源
        最近更新 更多