Mat*_*t J 11 asp.net-routing razor asp.net-mvc-3
当使用Url.Action
帮助程序自动生成Urls时,如果页面包含类似于的行
@ Url.Action( "编辑", "学生")
预计将产生一个像domain/student/edit
它一样的URL 并且它按预期工作.但是,如果请求的URL包含一些参数,例如domain/student/edit/210
,上面的代码使用前一个请求中的这些参数并生成类似的东西,即使我没有为该Action
方法提供任何此类参数.
简而言之,如果请求的网址包含任何参数,则无论我是否在Url.Action
方法中指定,任何自动生成的网页链接(为该请求提供服务)都将包含这些参数.
出了什么问题?
WEF*_*EFX 19
使用Darin的答案来自这个类似的问题.
@Url.Action("Edit","Student", new { ID = "" })
Run Code Online (Sandbox Code Playgroud)
很奇怪,似乎无法重现问题:
public class HomeController : Controller
{
public ActionResult Index(string id)
{
return View();
}
public ActionResult About(string id)
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
在里面Index.cshtml
:
@Url.Action("About", "Home")
Run Code Online (Sandbox Code Playgroud)
现在当我请求/home/index/123
url helper /home/about
按预期生成时.没有幽灵参数.那么你的场景有何不同?
更新:
既然您已经阐明了您的方案,那么您似乎有以下内容:
public class HomeController : Controller
{
public ActionResult Index(string id)
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
在Index.cshtml
你内心尝试使用:
@Url.Action("Index", "Home")
Run Code Online (Sandbox Code Playgroud)
如果您请求/home/index/123
此生成/home/index/123
而不是预期/home/index
(或简单地/
考虑默认值).
此行为是设计使然.如果要更改它,则必须编写自己的帮助程序,忽略当前路径数据.以下是它的外观:
@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
)
Run Code Online (Sandbox Code Playgroud)
这将生成您期望的正确URL.当然这很难看.我建议你将它封装成一个可重用的帮助器.
归档时间: |
|
查看次数: |
28670 次 |
最近记录: |