开发者

T4MVC Doesn't work property with Url.Action()

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/Login开发者_运维百科Youtube

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?


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: 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));
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜