我正在使用Html.ActionLink生成链接。
@ Html.ActionLink(item.Text,item.Author.UniqueName,“作者”,新{selectedQuoteId = item.QuoteID},null)
但是,当我使用此方法时,生成的链接为:
http://localhost/Author/aeschylus?selectedQuoteId=1627
但我希望如此:
http://localhost/Author/aeschylus/1627
我的配置是:
routes.MapRoute( “AuthorQuotes” “作者/ {authorUniqueName} / {} selectedQuoteId”, 新 { controller =“AuthorQuotes”, action =“索引”, authorUniqueName = UrlParameter.Optional, selectedQuoteId = UrlParameter.Optional });
使用Html.ActionLink可以做到这一点吗?
答案 0 :(得分:3)
确保您已将此自定义路线置于之前。此外,您似乎忘记为路线的authorUniqueName
部分提供值。由于这不是路由模式的最后一部分,因此不能是可选的。
首先修复你的路线定义:
routes.MapRoute(
"AuthorQuotes",
"Author/{authorUniqueName}/{selectedQuoteId}",
new {
controller = "AuthorQuotes",
action = "Index",
selectedQuoteId = UrlParameter.Optional
}
);
然后为此必需参数提供值:
@Html.ActionLink(
item.Text,
item.Author.UniqueName,
"Author",
new {
authorUniqueName = item.Author.UniqueName,
selectedQuoteId = item.QuoteID
},
null
)
基本上,如果你想在路线中有可选参数,这只能是最后一个。如果您需要有多个可选参数,那么请使用查询字符串(就像您一样),否则路由引擎无法消除歧义。