我想創建一些HTML幫助器函數來創建一些帶有生成HTML內容的鏈接。我會盡可能遵循默認的API。當我想傳遞一個routevalues對象時,這會變得棘手。一個類型爲RouteValueDictionary
的routevalue對象是(有意?)在MVC中創建麻煩。我想通過object routevalues
,就像Html.ActionLink
那樣。棘手的部分是,我似乎需要UrlHelper.CreateUrl
需要RouteValueDictionary
。我查看了ActionLink
如何在內部執行此操作,並使用TypeHelper.ObjectToDictionary
。然而,這是一個內部類,所以我不能訪問它。我可以複製粘貼這些東西,但除此之外,首先我會違反許可證,如果我這樣做,並且沒有根據Apache 2.0許可證或兼容許可證進行許可,其次,複製粘貼編程會給我heeby- jeebies。用html內容創建鏈接
下面是我想大概喜歡做的事:
public static MvcHtmlString MyFancyActionLink(this HtmlHelper helper,
Foo foo,
object routevalues){
TagBuilder inner = fancyFooContent(foo);
RouteValueDictionary routedict = TypeHelper.ObjectToDictionary(routevalues);
//alas! TypeHelper is internal!
string url = UrlHelper.GenerateUrl(null,
"myaction",
"mycontroller",
routedict,
helper.ViewContext.RequestContext,
true);
TagBuilder link = new TagBuilder("a");
link.MergeAttribute("href", url);
link.InnerHtml = inner.toString();
return MvcHtmlString.Create(link.ToString());
}
你能更新你的版本嗎? https://aspnetwebstack.codeplex.com/workitem/1737 – RobH 2014-10-27 11:47:50