Razor Asp.Net Core 2.1.0-preview1-final:@Html.ActionLink()不适用于string.Format() @Html.Raw(string.Format(@_stringLocalizer[“RegisterNoticeMessage”],@Html.ActionLink(@_stringLocalizer[“RegisterLinkDisplayName”],“Register”))

Razor Asp.Net Core 2.1.0-preview1-final:@Html.ActionLink()不适用于string.Format() @Html.Raw(string.Format(@_stringLocalizer[“RegisterNoticeMessage”],@Html.ActionLink(@_stringLocalizer[“RegisterLinkDisplayName”],“Register”)),razor,asp.net-core-mvc,asp.net-core-2.1,Razor,Asp.net Core Mvc,Asp.net Core 2.1,在这段代码中,@Html.ActionLink()将返回Microsoft.AspNetCore.Mvc.Rendering.TagBuilder,而不是返回包含指定操作的URL路径的锚元素。 在string.Format()中使用@Html.ActionLink()的正确方法是什么。或者,我在这里遗漏了什么吗?助手方法Html.ActionLink总是返回一个TagBuilder对象。将此类对象传递到字符串参数时,将调用ToString()方法,从而生成观察到的输出(类名:“Microsoft

在这段代码中,@Html.ActionLink()将返回Microsoft.AspNetCore.Mvc.Rendering.TagBuilder,而不是返回包含指定操作的URL路径的锚元素。
string.Format()中使用@Html.ActionLink()的正确方法是什么。或者,我在这里遗漏了什么吗?

助手方法
Html.ActionLink
总是返回一个
TagBuilder
对象。将此类对象传递到
字符串
参数时,将调用
ToString()
方法,从而生成观察到的输出(类名:“Microsoft.AspNetCore.Mvc.Rendering.TagBuilder”)

在我看来,你试图以一种相当奇怪的方式创建一个超链接。您是否尝试过使用
Url.Action
helper方法?此方法返回一个普通的旧
字符串
,可以插入任何href属性

例如,此代码将与您试图实现的目标等效:

<div data-collapse class="left-justify" id="requirements">
  @Html.Raw(string.Format(@_stringLocalizer["RegisterNoticeMessage"], @Html.ActionLink(@_stringLocalizer["RegisterLinkDisplayName"], "Register")))
</div>
@Html.Raw(
格式(_stringLocalizer[“RegisterNoticeMessage”],
"")
)
旁注:

  • 可以获取标记生成器的
    字符串
    值,如post中所示
  • 当您已经在Razor/C#环境中工作时,无需重复
    @
  • 使用
    Html.Raw
    时要格外小心,因为这可能会导致XSS漏洞
@Html.Raw(
    string.Format(_stringLocalizer["RegisterNoticeMessage"], 
    "<a href=\"" + Url.Action("Register") + "\">" + _stringLocalizer["RegisterLinkDisplayName"] + "</a>")
)