使用以下代码:
@Html.ActionLink("News", "Index", new { controller = "News", area = "" }, new { @class = "News" })我得到了一个错误,所以当我在一篇特定的新闻文章上时,链接生成为:
http://mywebsite/News/2011/12/2/my_slug
其中的参数与我当前所在的页面匹配。这也发生在匹配相同url模式的其他控制器上(假设我有另一个控制器,名为Presentation,它完全匹配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还是我的路由定义上吗?
编辑:
将操作链接更改为:
@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&month=12&slug=seo_overview
不确定为什么这会导致重写发生变化。
发布于 2011-12-06 03:19:45
由于未提供任何插件,因此操作链接将采用当前的插件:
使用
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 =空字符串。
https://stackoverflow.com/questions/8390570
复制相似问题