ASP.Net MVC 3 Url.Action方法使用的参数值从previous要求 [英] ASP.Net Mvc 3 Url.Action method uses parameter values from previous request
问题描述
在网址中使用 Url.Action
助手自动生成的,如果一个网页包含类似于行
When Urls are autogenerated using the Url.Action
helper, if a page contains a line similar to
@ Url.Action(编辑,学生)
@Url.Action("Edit","Student")
有望如预期产生像域/学生/编辑
及其工作的URL。
但是,如果请求的URL包含一些参数,如域/学生/编辑/ 210
,上述code从previous要求使用这些参数,产生的东西即使我没有提供任何这样的参数在动作
的方法类似。
is expected to generate a url like domain/student/edit
and its working as expected.
But if the requested url contains some parameters, like domain/student/edit/210
, the above code uses these parameters from the previous request and generates something similar even though I've not provided any such parameter to the Action
method.
总之,如果请求的URL中包含任何参数,任何自动生成的页面(即要求供应)的链接将包括那些参数,以及无论我在 URL指定与否.Action
方法。
In short, if the requested url contains any parameters, any auto generated links of the page (served for that request) will include those parameters as well no matter if I specify them or not in the Url.Action
method.
什么错?
推荐答案
怪异,似乎无法重现该问题:
Weird, can't seem to reproduce the problem:
public class HomeController : Controller
{
public ActionResult Index(string id)
{
return View();
}
public ActionResult About(string id)
{
return View();
}
}
和里面的 Index.cshtml
:
@Url.Action("About", "Home")
现在,当我要求 /家庭/指数/ 123
的网址助手生成如预期的/ home /约
。没有鬼的参数。那么,如何您的情况有所不同?
Now when I request /home/index/123
the url helper generates /home/about
as expected. No ghost parameters. So how does your scenario differs?
更新:
现在你已经阐明你的情况看来你具备以下条件:
Now that you have clarified your scenario it seems that you have the following:
public class HomeController : Controller
{
public ActionResult Index(string id)
{
return View();
}
}
和里面的 Index.cshtml
你要使用:
@Url.Action("Index", "Home")
如果您要求 /家庭/指数/ 123
这会产生 /家庭/指数/ 123
而不是预期 /家庭/指数
(或简称 /
考虑到默认值)。
If you request /home/index/123
this generates /home/index/123
instead of the expected /home/index
(or simply /
taken into account default values).
这行为是设计使然。如果你想改变它,你将不得不写了自己的助手而忽略当前的路由数据。下面是它可能的:
This behavior is by design. If you want to change it you will have to write your own helper which ignores the current route data. Here's how it might look:
@UrlHelper.GenerateUrl(
"Default",
"index",
"home",
null,
Url.RouteCollection,
// That's the important part and it is where we kill the current RouteData
new RequestContext(Html.ViewContext.HttpContext, new RouteData()),
false
)
这将产生您所期望正确的URL。当然,这是丑陋的。我建议你打包成一个可重用的帮手。
This will generate the proper url you were expecting. Of course this is ugly. I would recommend you encapsulating it into a reusable helper.
这篇关于ASP.Net MVC 3 Url.Action方法使用的参数值从previous要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!