ASP.NET MVC URL路由问题

发布于 2024-09-03 03:03:52 字数 1918 浏览 3 评论 0原文

我定义了一个路由如下:

context.MapRoute("SearchEngineWebSearch", "search/web/{query}/{index}/{size}",
                            new
                            {
                                controller = "search",
                                action = "web",
                                query = "",
                                index = 0,
                                size = 5
                            });

和处理请求匹配的操作方法:

public System.Web.Mvc.ActionResult Web(string query = "", int index = 0, int size = 5)
    {
        if (string.IsNullOrEmpty(query))
            return RedirectToRoute("SearchEngineBasicSearch");
        var search = new Search();
        var results = search.PerformSearch(query, index, size);
        ViewData["Query"] = query;
        if (results != null && results.Count() > 0)
        {
            ViewData["Results"]= results;
            return View("Web");
        }
        else return View("Not-Found");
    }

以及将参数发送到操作方法的表单:

<% using (Html.BeginForm("Web", "Search", FormMethod.Post))
       { %>
    <input name="query" type="text" value="<%: ViewData["Query"]%>" class="search-field" />
    <input type="submit" value="Search" class="search-button" />
    <input type="hidden" name="index" value="2" />
    <input type="hidden" name="size" value="2" />
    <%} %>

现在单击提交并将值发送到操作方法后,所有路由值都会更新,但网址值仍然等于第一个发送参数的时间。例如,如果我发送第一次请求,例如 http://localhost/search/web/google下一次http://localhost/search/web/yahoo,传递给操作的查询参数方法是yahoo,但回发后的url仍然是 http://localhost/search/web/google

有人可以帮我吗? ;)

i have defined a route as below:

context.MapRoute("SearchEngineWebSearch", "search/web/{query}/{index}/{size}",
                            new
                            {
                                controller = "search",
                                action = "web",
                                query = "",
                                index = 0,
                                size = 5
                            });

and action method to handle request match with that:

public System.Web.Mvc.ActionResult Web(string query = "", int index = 0, int size = 5)
    {
        if (string.IsNullOrEmpty(query))
            return RedirectToRoute("SearchEngineBasicSearch");
        var search = new Search();
        var results = search.PerformSearch(query, index, size);
        ViewData["Query"] = query;
        if (results != null && results.Count() > 0)
        {
            ViewData["Results"]= results;
            return View("Web");
        }
        else return View("Not-Found");
    }

and form to sent parameter to action method:

<% using (Html.BeginForm("Web", "Search", FormMethod.Post))
       { %>
    <input name="query" type="text" value="<%: ViewData["Query"]%>" class="search-field" />
    <input type="submit" value="Search" class="search-button" />
    <input type="hidden" name="index" value="2" />
    <input type="hidden" name="size" value="2" />
    <%} %>

now after click on submit and sending value to action method all route values updated but url values still is equals to first time of sending parameter. for example if i sent for first time request such as http://localhost/search/web/google and for next time http://localhost/search/web/yahoo, query parameter which passed to action method is yahoo but url after postback is http://localhost/search/web/google still!

can anybody help me plz? ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两相知 2024-09-10 03:03:52

尝试类似的方法

return RedirectToAction("Web", 
   new { query = query, index = index, size = size}); 

来代替 return View("Web");

另请注意,您应该执行 GET 而不是 POST。如果使用默认值提交,则索引和大小参数可以省略。

Try something like

return RedirectToAction("Web", 
   new { query = query, index = index, size = size}); 

instead of return View("Web");.

Also, note that you should perform a GET instead of a POST. And the index and size parameters may be ommitted if they were submitted with the default values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文