使用以下代码:
@Html.ActionLink("News", "Index", new { controller = "News", area = "" }, new { @class = "News" })
我得到一个错误,所以当我在一个特定的新闻文章,链接生成为:
http://mywebsite/News/2011/12/2/my_slug,其中参数与我当前所在的页面匹配。在匹配相同url模式的其他控制器上也会发生这种情况(假设我有另一个名为Presentations的控制器,它完全匹配News的url模式)。从演示页面,到新闻的链接将包括演示参数,这些参数不是新闻控制器的有效输入。有意义吗?
我的路由定义为:
routes.MapRoute(
"News-Archive", // Route name
"News/Archive", // URL with parameters
new { controller = "News", action = "Archive" }, // Parameter defaults
new[] { "mywebsite.Controllers" }
);
routes.MapRoute(
"News-Stub", // Route name
"News/{year}/{month}/{date}/{slug}", // URL with parameters
new { controller = "News", action = "Index" }, // Parameter defaults
new[] { "mywebsite.Controllers" }
);
routes.MapRoute(
"News-ByDay", // Route name
"News/{year}/{month}/{day}", // URL with parameters
new { controller = "News", action = "Archive" }, // Parameter defaults
new[] { "mywebsite.Controllers" }
);
routes.MapRoute(
"News-ByMonth", // Route name
"News/{year}/{month}", // URL with parameters
new { controller = "News", action = "Archive" }, // Parameter defaults
new[] { "mywebsite.Controllers" }
);
routes.MapRoute(
"News-ByYear", // Route name
"News/{year}", // URL with parameters
new { controller = "News", action = "Archive" }, // Parameter defaults
new[] { "mywebsite.Controllers" }
);
routes.MapRoute(
"News-Default", // Route name
"News", // URL with parameters
new { controller = "News", action = "Index" }, // Parameter defaults
new[] { "mywebsite.Controllers" }
);
有人可以解释如果问题在于我使用Html。ActionLink还是我的路由定义?
编辑:将Actionlink更改为:
@Html.ActionLink("News", "Index", new { controller = "News", area = "", year = string.Empty, month = string.Empty, day = string.Empty, slug = string.Empty}, null)
当从演示页面调用时(这些值用于演示,而不是有效的新闻项):
http://mywebsite/News?year=2011&月= 12,蛞蝓= seo_overview
不知道为什么会导致重写发生变化
动作链接假定为当前的段塞,因为没有提供段塞:
使用 ActionLink("News", "Index", new { controller = "News", area = "", slug="newslug"}, new { @class = "News" })
或
ActionLink("News", "Index", new { controller = "News", area = "", slug = null}, new { @class = "News" })
设置slug = null也有可能重用当前的slug,在这种情况下设置slug = Empty String