可能有更优雅的 MVC/路由解决方案,但简单的扩展方法应该可以解决问题:
public static string TokenActionLink(this HtmlHelper html,
string linkText,
string actionName,
string controllerName,
int id,
int token)
{
var anchorFormat = "<a href=\"{0}\">{1}</a>";
var urlFormat = "{0}/{1}/{2}?token={3}";
return string.Format(anchorFormat, string.Format(urlFormat, controllerName, actionName, id, token.ToString()), linkText);
}
用法:
<%: Html.TokenActionLink("Show Me", "Detail", "Product", Model.Id, Model.Token) %>
或者,也许您可以创建一个自定义 RouteValueDictionary:,并从您的自定义方法中调用常规 ActionLink 方法:
public static string TokenActionLink(this HtmlHelper html,
string linkText,
string actionName,
string controllerName,
int id,
int token)
{
var rvd = new RouteValueDictionary(ViewContext.RouteData.Values);
rvd["Token"] = token.ToString();
return Html.ActionLink(linkText, actionName, controllerName, id, rvd);
}