2011-06-05 53 views
4

工作性質這是我原來的代碼:T4MVC不能與Url.Action()

@Url.Action("LoginYoutube", "Account", new { returnUrl = Request.QueryString["ReturnUrl"] }, "http") 

這將產生:http://localhost:2543/Account/LoginYoutube

隨着T4MVC我做的:

Url.Action(MVC.Account.LoginYoutube().AddRouteValue("returnUrl", Request.QueryString["ReturnUrl"])) 

和生成:/帳戶/登錄Youtube

我需要與「http」的最後一個參數爲了得到http://localhost:2543。問題是T4MVC我只能把1個參數調用到Url.Action()。

我該如何得到這個工作?

回答

7

T4MVC確實在這裏丟失了一些東西,但它應該很容易添加。請嘗試以下。在T4MVC.tt,變化:

public static string Action(this UrlHelper urlHelper, ActionResult result) { 
     return urlHelper.RouteUrl(result.GetRouteValueDictionary()); 
    } 

public static string Action(this UrlHelper urlHelper, ActionResult result, string protocol = null, string hostName = null) { 
     return urlHelper.RouteUrl(null, result.GetRouteValueDictionary(), protocol, hostName); 
    } 

這應該允許你寫:

@Url.Action(MVC.Account.LoginYoutube().AddRouteValue("returnUrl", Request.QueryString["ReturnUrl"]), "http") 

請讓我知道如何工作的,所以我們可以決定是否在官方模板中更改此項。

+0

是的工作得很好!這也可能是一個很重要的做法,也可以通過舊的方式添加重載:Url.Action(MVC.Account.LoginYoutube(),new {Request.QueryString [「ReturnUrl」]},「http」) – Ryan 2011-06-05 15:26:18

+2

FYI I這個變化(2.6.55)昨晚推出了一個新的版本。 – 2011-06-07 05:21:54

4

@David Ebbo: FYI I pushed a new build last night with this change (2.6.55).

這實際上打破了MVCContrib網格。或者至少,現在與以前T4MVC工作的代碼我得到一個編譯錯誤:

CS0854: An expression tree may not contain a call or invocation that uses optional arguments

生成網格代碼:加入這個到.TT

Html.Grid(Model.Customers) 
      .Columns(c => 
      { 
       c.For(x => Html.ActionLink(x.Name, MVC.Partner.Edit(x.ID), new { @class = "ILPartnerEdit" })) 
        .Named(LanguageResources.Name); 
... 

但解決( < 3開源):

 public static <#=HtmlStringType #> ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, object htmlAttributes) 
     { 
      return ActionLink(htmlHelper, linkText, result, new RouteValueDictionary(htmlAttributes)); 
     } 
+0

我認爲這是更多有關的不同(類似)的問題:http://stackoverflow.com/questions/6280347/how-can-i-add-a-hash-fragment-to-t4mvc-route-dictionary-actionresult/ 6302703 – 2011-06-16 18:00:32

+0

但我不知道我明白爲什麼會打破,因爲我只添加了可選參數。你可以解釋嗎?謝謝! – 2011-06-16 18:05:39

+0

這是一個C#4「功能」。表達式中不能包含可選參數的方法。 http://lostechies.com/jimmybogard/2010/05/18/caveats-of-c-4-0-optional-parameters/ 編譯包含由表達式組成的網格的aspx視圖時,會出現編譯異常。 (我想這對剃刀是一樣的)。 – 2011-06-21 08:14:56