2012-03-09 36 views
1

在我的項目中,我試圖在區域內的視圖中創建一個URL。
的代碼是這樣的:T4MVC在使用區域時爲ActionLink創建不正確的URL

@Html.ActionLink("Cancel", MVC.MyArea.MyController.Index(), new { @class = "btn" }) 

當我查看結果在瀏覽器中生成的代碼如下:

<a class="btn" href="/MyArea/MyController/MyCurrentAction?Count=3&amp;Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&amp;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)); 
} 

現在,它的工作(這是偉大的,當然),至少對我做了測試,但我怕,我做錯了什麼首先,這條道路可能會導致我在未來遇到一些問題。

回答

1

該死的,這看起來像2.6.69的迴歸。你可以試試2.6.68來證實它以前的工作嗎?我現在隱藏了2.6.69,所以其他人不會在新項目中自動獲取它(並在更新時)。

這是引發不良修復bug:http://mvccontrib.codeplex.com/workitem/7191

此外,您可以嘗試在該錯誤的最後評論中提及確切的解決?將方法更改爲:

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(), HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); 
    } 

對不起!

+0

我改變了方法,因爲你指出它已被修復!順便說一句,你用T4MVC做了很棒的工作。謝謝! – 2012-03-09 20:22:59

+2

謝謝!好的,修正現在在新的2.7.0版本(在NuGet上),所以你應該能夠更新。 – 2012-03-09 20:55:14