Asp.net mvc 3 @资源文件中的ActionLink()?

Asp.net mvc 3 @资源文件中的ActionLink()?,asp.net-mvc-3,Asp.net Mvc 3,我想包括 @Html.ActionLink() 资源文件中的命令,但在调用资源时无法使其正确显示。基本上是这样的资源条目: <div>Click here to @Html.ActionLink("contact us", null)</div> 而不是 Click here to contact us 在我看来。有什么方法可以让razor标签被正确读取吗?我认为您不能在资源文件中嵌入实际的代码,并且期望视图引擎在渲染时调用它,它可能认为它只是一个字符串(不需要考虑更

我想包括 @Html.ActionLink() 资源文件中的命令,但在调用资源时无法使其正确显示。基本上是这样的资源条目:

<div>Click here to @Html.ActionLink("contact us", null)</div>
而不是

Click here to contact us

在我看来。有什么方法可以让razor标签被正确读取吗?

我认为您不能在资源文件中嵌入实际的代码,并且期望视图引擎在渲染时调用它,它可能认为它只是一个字符串(不需要考虑更多)

更好的方法是使用
string.Format

将资源存储为:

<div>Click here to {0}</div>
如果您经常这样做,您还可以使用自定义HTML帮助程序“使其更甜”:

public static MvcHtmlString ResourceBasedActionLink(this HtmlHelper htmlHelper, string resourceName, string linkText, string actionName, string controllerName, object htmlAttributes)
{
   var link = htmlhelper.ActionLink(linkText, actionName, controllerName, htmlAttributes);
   return MvcHtmlString.Create(string.Format(resourceName, link)));
}
然后:

@Html.ResourceBasedActionLink(Resources.Global.LinkHtmlFormat, "Contact Us", "Contact", Controller", null)

我不认为您可以在资源文件中嵌入实际代码,并且期望视图引擎在渲染时调用它,它可能认为它只是一个字符串(它不需要考虑更多)

更好的方法是使用
string.Format

将资源存储为:

<div>Click here to {0}</div>
如果您经常这样做,您还可以使用自定义HTML帮助程序“使其更甜”:

public static MvcHtmlString ResourceBasedActionLink(this HtmlHelper htmlHelper, string resourceName, string linkText, string actionName, string controllerName, object htmlAttributes)
{
   var link = htmlhelper.ActionLink(linkText, actionName, controllerName, htmlAttributes);
   return MvcHtmlString.Create(string.Format(resourceName, link)));
}
然后:

@Html.ResourceBasedActionLink(Resources.Global.LinkHtmlFormat, "Contact Us", "Contact", Controller", null)

好主意。我可以为某些常用链接定义标记,并在助手中自动替换它们,这样我就不必每次调用该方法时都依赖于传递参数。谢谢!另外,@string.Format(…)应该封装在一个@Html.Raw(…)中,以防止字符转义(根据:)这是个好主意。我可以为某些常用链接定义标记,并在助手中自动替换它们,这样我就不必每次调用该方法时都依赖于传递参数。谢谢!另外,@string.Format(…)应该封装在@Html.Raw(…)中,以防止字符转义(根据:)