【发布时间】:2011-11-19 21:24:21
【问题描述】:
在我的布局页面中,构成我网站的主要部分的链接通过如下调用呈现:
@SiteSectionLink("index", "blog", "blog")
SiteSectionLink 是一个看起来像这样的助手:
@helper SiteSectionLink(string action, string controller, string display)
{
<li>
<h1>
<a class="site-section" href="@Url.Action(action, controller)">@display</a></h1>
</li>
}
在实际的博客页面上,所有链接也引用“索引”操作,但还指定用于过滤帖子的日期参数(例如“blog/4-2011”或“blog/2010”)按日期期间。除此之外,还有一个可选的postID 参数用于引用特定的帖子。
为此,我有以下路线:
routes.MapRoute(
"Blog",
"blog/{date}/{postID}",
new
{
controller = "blog",
action = "index",
date = UrlParameter.Optional,
postID = UrlParameter.Optional
}
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
现在,问题是,当我单击类似于“blog/11-2010”或“blog/11-2010/253”的链接时,我的布局页面中的链接通常指向我的博客现在 也 当我希望它只链接到“blog/”而不是“blog/11-2010”时,它指的是同一个 URL。
如果我更改 SiteSectionLink 帮助器以显式为 date 和 postID 传递 null,如下所示:
<a class="site-section" href="@Url.Action(action, controller,
new { date = (string)null, postID = (int?)null})">@display</a></h1>
当前路由值仍在使用,但现在看起来像“blog?date=11-2010”。
我看到this 类似的问题,但接受的答案对我不起作用,我一开始不使用ActionLink,我怀疑ActionLink 会在后台使用Url.Action。
【问题讨论】:
标签: asp.net asp.net-mvc url-routing asp.net-mvc-routing