HTML 实体在从 ViewData 传入 HTML 时未转换为其结果

HTML Entity not converting to its result when passing into HTML from ViewData

当我通过视图数据对象将 HTML 实体传递给 HTML 时,它不会转换为它的结果。 (即 ™ 实体保持 ™ 并且不会转换为 ™)

这是目标页面

@{
    ViewData["Title"] = "Assess Mental Health";
    ViewData["HeaderTitle"] = "Assess Mental Health";
    ViewData["HeaderLine1"] = "with";
    ViewData["HeaderLine2"] = "Intelligent Assessments™";
    ViewData["HeaderImage"] = "url(../img/headers/AI_Head.png)";
}

<main data-zanim-timeline="{}" data-zanim-trigger="scroll">
    <partial name="_Header" />
</main>

这是部分

<!--
####################################################
H E A D E R
####################################################
-->
<section class="overflow-hidden py-0" id="top">
    <div class="bg-holder overlay parallax" style="background-image:@ViewData["HeaderImage"];background-position: center 58%;">
    </div>
    <!-- / bg-holder-->
    <div class="header-overlay"></div>
    <div class="container" data-zanim-xs='{"duration":"1.5","delay":"0.1"}'>
        <div class="d-flex align-items-center pt-10 pb-8">
            <div class="header-text">
                <div class="overflow-hidden">
                    <h1 class="display-3 text-white fs-4 fs-md-6">@ViewData["HeaderTitle"]</h1>
                    <p class="text-uppercase text-400 ls-2 mt-2 my-0 pb-0 pt-2">@ViewData["HeaderLine1"]</p>
                    <p class="my-0 py-0"><span class="fs-7 logo-secondary-style-light">pr<span class="logo-primary-style-light">e</span>vidence<sup class="fs-3 font-weight-light">&trade;</sup></span></p>
                    <p class="text-uppercase fs-1 text-400 ls-2 my-0 py-0">@ViewData["HeaderLine2"]</p>
                </div>
                <partial name="_CallToActionLight" />
                <div class="header-indicator-down"><a class="indicator indicator-down opacity-75" href="#Content" data-fancyscroll="data-fancyscroll" data-offset="64"><span class="indicator-arrow indicator-arrow-one" data-zanim-xs='{"from":{"opacity":0,"y":30},"to":{"opacity":1,"y":0},"ease":"Back.easeOut","duration":1,"delay":1.5}'></span></a></div>
            </div>
        </div>
    </div>
</section>
<!-- / end  H E A D E R -->

这是我的布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="robots" content="noindex">
    <title>@ViewBag.Title | Previdence | Mental Healthcare Platform</title>
    <partial name="_Favicons" />
    <link href="https://fonts.googleapis.com/css?family=Josefin+Sans&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <script src="https://kit.fontawesome.com/971f8efccd.js" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link href="~/lib/non-npm/owl.carousel/owl.carousel.css" rel="stylesheet">
    <link href="~/lib/non-npm/remodal/remodal.css" rel="stylesheet">
    <link href="~/lib/non-npm/remodal/remodal-default-theme.css" rel="stylesheet">
    <link href="~/lib/non-npm/theme/theme.css" rel="stylesheet">
    <link rel="stylesheet" href="~/css/site.css" />
    <link rel="stylesheet" href="~/css/cookiealert.css" />
</head>
<body>
    <partial name="_Preloader" />
    <partial name="_TopNavbar" />
    @*<partial name="_CookieConsentPartial" />*@
    @RenderBody()
    <partial name="_Footer" />
    <partial name="_GDPR_Alert" />
    <!--
    ####################################################
    J A V A S C R I P T - jquery, bootstrap, plugins
    Place at the end of the document so the pages load faster
    ####################################################
    -->
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    @RenderSection("Scripts", required: false)
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/lib/non-npm/plugins/plugins.min.js"></script>
    <script src="~/lib/non-npm/stickyfilljs/stickyfill.min.js"></script>
    <script src="~/lib/non-npm/fortawesome/all.min.js"></script>
    <script src="~/lib/non-npm/rellax/rellax.min.js"></script>
    <script src="~/lib/non-npm/progressbar/progressbar.min.js"></script>
    <script src="~/lib/non-npm/isotope-layout/isotope.pkgd.min.js"></script>
    <script src="~/lib/non-npm/isotope-packery/packery-mode.pkgd.min.js"></script>
    <script src="~/lib/non-npm/owl.carousel/owl.carousel.js"></script>
    <script src="~/lib/non-npm/remodal/remodal.min.js"></script>
    <script src="~/lib/non-npm/hover-dir/jquery.hoverdir.js"></script>
    <script src="~/lib/non-npm/typed/typed.js"></script>
    <script src="~/lib/non-npm/theme/theme.min.js"></script>
    <script src="~/lib/non-npm/GDPR_Alert/cookiealert.js"></script>

    <!-- / end J A V A S C R I P T -->
</body>
</html>

尝试使用 @Html.Raw(string) 渲染 html 字符串:

@Html.Raw(@ViewData["HeaderLine2"])

感谢@Nan Yu 上面的回答,让我找到了答案。

Try with @Html.Raw(string) to render html string

@Html.Raw() 是答案的一部分,另一部分是我正在使用 text-uppercase 文本转换,所以我还需要添加一个 text-transform-none 来删除 text-uppercase 文本转换(此文本转换未在我的代码示例中显示)。

@Html.Raw(<span class=\"text-transform-none\">&trade;</span>)

我现在得到正确的输出,其中 &trade; 输出是 ™ 而不是 &trade;