Blazor Server端格式标记带有嵌入式样式的标记标记,而忽略CSS文件样式

发布于 2025-02-02 04:26:04 字数 1121 浏览 2 评论 0原文

我的Blazor服务器端应用程序发布了从Web API核心获得的丰富内容。
内容带有任意HTML标签,其中一组有限的CSS类。
大餐应根据这些类型样式,但在匹配的CSS文件中声明时忽略了样式。
但是,如果样式嵌入了同一剃须刀页面中,它将成功地格式化内容。

练习后表明了这个问题。
是否有任何解决方法可以将样式保留在CSS文件上并相应地格式化标记内容?

test.razor页面,带有嵌入式样式:

@page "/Test"

<div class="FromRazorStyle">
    Razor hard coded text, embedded razor style
</div>
<div class="FromCssFile">
    Razor hard coded text, style from Css file
</div>

@((MarkupString)FromRazorStyle)
@((MarkupString)FromCssFile)

<style scoped>
    .FromRazorStyle {
        font-weight:600;
        margin-bottom:20px;
    }
</style>

@code {
    string FromRazorStyle = "<div class='FromRazorStyle'>Markup string, embedded razor style<br/></div>";
    string FromCssFile = "<div class='FromCssFile'>Markup string, style from Css file<br/></div>";
}

test.razor.css文件:

.FromCssFile {
    font-weight: 600;
    margin-bottom: 20px;
}

结果:
剃须刀硬编码文本,嵌入式剃须刀样式
剃须刀硬编码文本,CSS文件的样式
标记字符串,嵌入式剃须刀样式

标记字符串,CSS文件的样式

My Blazor Server side application posts rich content obtained from a Web Api Core.
The content comes with arbitrary Html tags with a limited set of well known Css classes.
Blazor should style the content according to these classes, but it ignores the styles when declared in the matching css file.
However, it successfully formats the content if the style is embedded in the same razor page.

Following exercise demonstrates the problem.
Is there any workaround to keep the styles on the css file and have the markup content formatted accordingly?

The Test.razor page, with embedded style:

@page "/Test"

<div class="FromRazorStyle">
    Razor hard coded text, embedded razor style
</div>
<div class="FromCssFile">
    Razor hard coded text, style from Css file
</div>

@((MarkupString)FromRazorStyle)
@((MarkupString)FromCssFile)

<style scoped>
    .FromRazorStyle {
        font-weight:600;
        margin-bottom:20px;
    }
</style>

@code {
    string FromRazorStyle = "<div class='FromRazorStyle'>Markup string, embedded razor style<br/></div>";
    string FromCssFile = "<div class='FromCssFile'>Markup string, style from Css file<br/></div>";
}

The Test.razor.css file:

.FromCssFile {
    font-weight: 600;
    margin-bottom: 20px;
}

Result:
Razor hard coded text, embedded razor style
Razor hard coded text, style from Css file
Markup string, embedded razor style

Markup string, style from Css file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高速公鹿 2025-02-09 04:26:04

是的,这是可能的,但不一定容易!问题在于,CSS隔离过程在.razor.css文件中重写选择器以包括随机生成的唯一标识符。因此,示例中的CSS实际上变成了.fromcssfile [B-C2X6L882ML]。此标识符还添加为.RAZOR文件中存在的所有HTML标签的属性,但不在标记字符串中的标签中。

一种解决方法是在项目文件中手动设置此CSSSCOPE标识符,如此页面,然后将其插入标记内容标签中。

但是,在这种情况下,一种更简单的方法可能是将当前的CSS放入静态文件中,例如默认的css/site.cs.css,例如:

.FromCssFile * {
    font-weight: 600;
    margin-bottom: 20px;
}

Yes, this is possible, but not necessarily easy! The problem is that the CSS isolation process rewrites the selectors in .razor.css files to include a randomly generated unique identifier. So, the CSS in your example actually becomes something like .FromCssFile[b-c2x6l882ml]. This identifier is also added as an attribute to all HTML tags present in the .razor file, but not to those in the markup string.

One workaround is to manually set this CssScope identifier in the project file, as explained on this page, and then also insert it into the markup content tags.

However, a simpler way in this case is probably to just put your current CSS in a static file, such as the default css/site.css, like this:

.FromCssFile * {
    font-weight: 600;
    margin-bottom: 20px;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文