ASP.NET MVC URL路由问题
我定义了一个路由如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试类似的方法
来代替
return View("Web");
。另请注意,您应该执行 GET 而不是 POST。如果使用默认值提交,则索引和大小参数可以省略。
Try something like
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.