4

This was my original code:

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

Which would generate: http://localhost:2543/Account/LoginYoutube

With T4MVC I do:

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

and that generates: /Account/LoginYoutube

I need the last parameter with "http" in order to get the http://localhost:2543. The problem is with T4MVC I can only put 1 parameter for the call to Url.Action().

How can I get this to work?

tereško
  • 58,060
  • 25
  • 98
  • 150
Ryan
  • 4,354
  • 2
  • 42
  • 78

2 Answers2

7

T4MVC is indeed missing something here, but it should be easy to add. Please try the following. In T4MVC.tt, change:

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

to

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

This should allow you to write:

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

Please let me know how that works, so we can decide whether to change this in the official template.

David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • Yep worked great! It might also be some value to add an overload for doing it the old fashiond way too: Url.Action(MVC.Account.LoginYoutube(), new { Request.QueryString["ReturnUrl"] }, "http") – Ryan Jun 05 '11 at 15:26
  • 2
    FYI I pushed a new build last night with this change (2.6.55). – David Ebbo Jun 07 '11 at 05:21
4

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

That actually breaks MVCContrib grid. Or at least with the code that worked with the previous T4MVC now I got a compilation error:

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

The code for generating the grid:

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

But solved by adding this to the .TT (<3 open source):

        public static <#=HtmlStringType #> ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, object htmlAttributes)
        {
            return ActionLink(htmlHelper, linkText, result, new RouteValueDictionary(htmlAttributes));
        }
Akos Lukacs
  • 2,007
  • 1
  • 16
  • 21
  • I think that's more related to a different (similar) issue: http://stackoverflow.com/questions/6280347/how-can-i-add-a-hash-fragment-to-t4mvc-route-dictionary-actionresult/6302703 – David Ebbo Jun 16 '11 at 18:00
  • But I'm not sure I understand why that breaks, since I only added optional params. Can you explain? Thanks! – David Ebbo Jun 16 '11 at 18:05
  • It's a C#4 "feature". You can't have methods with optional parameters in expressions. http://lostechies.com/jimmybogard/2010/05/18/caveats-of-c-4-0-optional-parameters/ I get a compile exception when compiling the aspx view that contains the the grid composed with expressions. (i guess it would be the same for razor). – Akos Lukacs Jun 21 '11 at 08:14
  • 1
    Got it! I just pushed that change in T4MVC 2.6.57. Please confirm that the new official template works for you. Thanks! – David Ebbo Jun 21 '11 at 19:09