无法在 Razor 组件中呈现原始 html

Cannot render raw html in a Razor component

我在 ASP.Core 3.0 razor pages(正常)项目中有一个以下 Razor 组件(Blazor 服务器端?):

@using WEKA.Models
@using Microsoft.AspNetCore.Html

<div class="jobs-list">
    @foreach (var e in News)
    {
        <div class="job" data-aos="fade-up">
            <a href="@e.Link">
                <div class="col name">@e.Text)</div>
                <div class="col more">Detail</div>
            </a>
        </div>
    }
</div>

@code {

    public List<QNewsList> News
    {
        set { }
        get
        {
            using (WEKAContext db = new WEKAContext())
            {
                var q = from n in db.Qaktuality select new QNewsList() { Datum = n.Datum.ToString("d.M.YYYY"), Text = new HtmlString(n.Text), Link = n.RssLink };
                return q.ToList();
            }
        }
    }

    public class QNewsList
    {
        public string Datum;
        public HtmlString Text;
        public string Link;
    }
}

该组件在普通 cs 中呈现html razor 页面:

@(await Html.RenderComponentAsync<WEKA.Components.NewsList>(RenderMode.Static))

我正在尝试使数据库字段 Text 在网页上显示为原始 HTML(即将 &nbsp; 呈现为 space)。 Html 对象在此处无法调用 Html.Raw。 上面的代码不会从数据库中呈现原始 html。我该如何解决?

当你使用它时它应该工作:

<div class="col name">@((MarkupString)e.Text)</div>

查看文档here,根据@Hrnk 所说,您需要在剃须刀 component.Change 代码中使用 MarkupString 而不是 HtmlString [=16] =]

@using WEKA.Models
@using Microsoft.AspNetCore.Html

<div class="jobs-list">
    @foreach (var e in News)
    {
        <div class="job" data-aos="fade-up">
            <a href="@e.Link">
                <div class="col name">@e.Text</div>
                <div class="col more">Detail</div>
            </a>
        </div>
    }
</div>

@code {

    public List<QNewsList> News
    {
        set { }
        get
        {
            using (WEKAContext db = new WEKAContext())
            {
                var q = from n in db.Qaktuality select new QNewsList() { Datum = n.Datum.ToString("d.M.YYYY"), Text = new MarkupString(n.Text), Link = n.RssLink };
                return q.ToList();
            }
        }
    }

    public class QNewsList
    {
        public string Datum;
        public MarkupString Text;
        public string Link;
    }
}

参考 https://github.com/aspnet/Blazor/issues/167

https://github.com/aspnet/AspNetCore/issues/12615