1
在我的項目中,我試圖在區域內的視圖中創建一個URL。
的代碼是這樣的:T4MVC在使用區域時爲ActionLink創建不正確的URL
@Html.ActionLink("Cancel", MVC.MyArea.MyController.Index(), new { @class = "btn" })
當我查看結果在瀏覽器中生成的代碼如下:
<a class="btn" href="/MyArea/MyController/MyCurrentAction?Count=3&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D">Cancel</a>
我去尋找在T4MVC代碼,並發現這是該方法產生上面的代碼(T4Extensions
類中):
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, object htmlAttributes, string protocol = null, string hostName = null, string fragment = null) {
return htmlHelper.RouteLink(linkText, null, protocol, hostName, fragment, result.GetRouteValueDictionary(), htmlAttributes);
}
顯然,RouteLink
方法不是b eing能夠使用result.GetRouteValueDictionary()
。
所以,我檢查了ASP.NET MVC source code並試圖複製相同的功能。我已經改變了我T4MVC這樣:
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, object htmlAttributes, string protocol = null, string hostName = null, string fragment = null) {
var t4mvcResult = result.GetT4MVCResult();
var actionName = t4mvcResult.Action;
var controllerName = t4mvcResult.Controller;
var routeValues = new RouteValueDictionary(t4mvcResult.RouteValueDictionary);
var htmlAttribs = new RouteValueDictionary(htmlAttributes);
return new MvcHtmlString(HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection, linkText, null /* routeName */, actionName, controllerName, protocol, hostName, fragment, routeValues, htmlAttribs));
}
現在,它的工作(這是偉大的,當然),至少對我做了測試,但我怕,我做錯了什麼首先,這條道路可能會導致我在未來遇到一些問題。
我改變了方法,因爲你指出它已被修復!順便說一句,你用T4MVC做了很棒的工作。謝謝! – 2012-03-09 20:22:59
謝謝!好的,修正現在在新的2.7.0版本(在NuGet上),所以你應該能夠更新。 – 2012-03-09 20:55:14