resx文件中的Razor语法

时间:2016-04-27 07:38:19

标签: c# razor resx

我在视图中有以下剃刀语法:

(@Html.ActionLink(SQPStrings.ForgotPassword, "ResetPassword", "User", new { Area = "Authentication" }, new { }))

在resx文件中,我有以下文字:

  

密码:使用您的密码(区分大小写)(在此处插入actionlink)

我似乎无法找到如何继续这一点,有人可以帮助我朝着正确的方向前进吗?我知道我需要在视图中执行String.Format并添加参数,但不是如何添加参数。

2 个答案:

答案 0 :(得分:2)

您可以在resx文件的字符串值中为string.Format定义占位符:

Password: use your password (case sensitive) ({0})

然后将值格式化为Razor视图中的已翻译字符串。 使用Html.Raw呈现生成的字符串或其中包含的<a href=".."\>将被转义。

var actionLink = Html.ActionLink("Link name" , "ResetPassword", "User", new { Area = "Authentication" }, new { }));
var resetDisplay = string.Format(SQPStrings.ForgotPassword, actionLink);
@Html.Raw(resetDisplay) 

答案 1 :(得分:1)

您可以更改.resx文件中的文本,将“在此处插入actionlink”部分替换为占位符{0},如下所示:

<data name="ForgotPassword" xml:space="preserve">
  <value>Password: use your password (case sensitive) ({0})</value>
</data>

在视图中,您应该将string.Format()Html.Row()助手结合使用,如下所示:

@Html.Raw(string.Format(SQPStrings.ForgotPassword, Html.ActionLink(link, "ResetPassword", "User", new { Area = "Authentication" }, new { }))))

其中link是您链接的字符串名称。