Html.ActionLink 包括额外参数
使用以下代码:
@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&month=12&slug=seo_overview
不确定为什么会导致重写发生变化。
Using the following code:
@Html.ActionLink("News", "Index", new { controller = "News", area = "" }, new { @class = "News" })
I'm getting an error so that when I'm on a particular news article, the link is generating as:
http://mywebsite/News/2011/12/2/my_slug
where the parameters match the page that I'm currently on. The also happens on other controllers matching the same url schema (imagine I have another controller called Presentations that matches the url scheme of News entirely). From the presentations pages, a link to news would include the presentations parameters which aren't valid input for the news controller. Make sense?
I have my routes defined as:
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" }
);
Can someone please explain if the problem lies in my use of Html.ActionLink or my route definitions?
EDIT:
Changing the Actionlink to:
@Html.ActionLink("News", "Index", new { controller = "News", area = "", year = string.Empty, month = string.Empty, day = string.Empty, slug = string.Empty}, null)
results in this when called from a presentation page (those values are for the presentation, not a valid news item):
http://mywebsite/News?year=2011&month=12&slug=seo_overview
Not sure why that's causing the rewriting to change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于没有提供 slug,操作链接假定当前的 slug:
使用
或
设置 slug = null 也有可能重新使用当前的 slug,在这种情况下设置 slug = 空字符串。
The action link assumes the current slug since no slug was provided:
Use
or
There is a chance that setting the slug = null would also re-use the current slug, in that case set the slug = Empty String.