1

为了在可翻译文本中创建 URL,我们将{0}其用作占位符。

所以我们这样做:

@string.Format(@translationHelper.GetTranslation("label-ClickToContinue"),
    @Html.ActionLink(
        @translationHelper.GetTranslation("text-here"),
               "Login",
               new { Model.UserName, Model.UniqueId }
    )
)

翻译键:

label-ClickToContinue = "Click {0} to continue"
text-here = "here"

但这会将转义的字符串打印到源代码中:<a href="/Login/Login?UserName=alberttest3&UniqueId=f3647fed-bab4-4575-bb5f-98ed27edff43">label-RequestNewOfficeWizard</a>

如何确保它不会显示 html-tag,而是显示 URL?

4

2 回答 2

1

只需将输出从string.Format()内部放入Html.Raw()如下所示:

@Html.Raw(string.Format(@translationHelper.GetTranslation("label-ClickToContinue"),
       @Html.ActionLink(
            @translationHelper.GetTranslation("text-here"),
            "Login",
            new { Model.UserName, Model.UniqueId }
       )
))

翻译键:

label-ClickToContinue = "Click {0} to continue"
text-here = "here"
于 2013-02-05T13:09:50.030 回答
0

ActionLink 帮助程序不支持此操作。它总是对文本进行 HTML 编码。您可以编写自己的不对文本进行编码的自定义助手,也可以简单地:

<a href="@Url.Action(Login, new { UserName, Model.UniqueId })">
    @Html.Raw(translationHelper.GetTranslation("text-here"))
</a>

顺便说一句,现在我已经发布了这段代码,它看起来很丑。继续编写自定义助手:

public static class HtmlExtensions
{
    public static IHtmlString ActionLinkLocalized(
        this HtmlHelper html, 
        string translationText, 
        string actionName, 
        object routeValues
    )
    {
        var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
        var anchor = new TagBuilder("a");
        anchor.Attributes["href"] = urlHelper.Action(actionName, routeValues);
        anchor.InnerHtml = TranslationHelper.GetTranslation(translationText);
        return new HtmlString(anchor.ToString());
    }
}

在您看来,简单地说:

@Html.ActionLinkLocalized("text-here", "Login", new { UserName, Model.UniqueId })
于 2013-02-05T12:28:39.313 回答