我如何在全球范围内设置自定义的htmlencoder以在所有剃须刀页面中使用?

发布于 2025-01-31 11:01:01 字数 867 浏览 2 评论 0原文

我想设置一个自定义htmlencoder为默认htmlencoder要在ASP.NET Core Razor页面中使用。

razorPageBase 类确实具有htmlencoder属性,我可以在单个剃须刀页面上设置该属性:

using System.Text.Encodings.Web;
using System.Text.Unicode;
@{
    HtmlEncoder = HtmlEncoder.Create(UnicodeRanges.All);
}

然后在该页面上使用htmlencoder

现在要尝试的显而易见的是将此代码应用于'viewStart.cshtml':

using System.Text.Encodings.Web;
using System.Text.Unicode;
@{
    Layout = "_Layout";
    HtmlEncoder = HtmlEncoder.Create(UnicodeRanges.All);
}

但是,这仍然不起作用,它仍然使用默认htmlencoder(它填充了良好,并且确实选择了布局属性)。

这意味着,要为所有页面设置默认htmlencoder,我必须将上面的代码包括到每个剃须刀页面,包括每个部分页面,当然还有“ _layout.cshtml”。

我希望我错过了什么。

我该如何在全球范围内指定它,因此它适用于所有剃须刀页面?

I want to set a custom HtmlEncoder as the default HtmlEncoder to be used in Asp.net Core Razor pages.

The RazorPageBase class does have a HtmlEncoder property which I can set on a single Razor page like this:

using System.Text.Encodings.Web;
using System.Text.Unicode;
@{
    HtmlEncoder = HtmlEncoder.Create(UnicodeRanges.All);
}

Then the HtmlEncoder is used on that page.

Now the obvious thing to try is to apply this code to 'ViewStart.cshtml':

using System.Text.Encodings.Web;
using System.Text.Unicode;
@{
    Layout = "_Layout";
    HtmlEncoder = HtmlEncoder.Create(UnicodeRanges.All);
}

However, this does not work, it still uses the default HtmlEncoder (it compiles fine and it does pick up the Layout property).

This means, that to set the default HtmlEncoder for all pages, I have to include the code above to each and every Razor page, including each and every Partial page and of course "_Layout.cshtml".

I hope I have missed something.

How can I specify this on a global scale so it applies to all Razor pages?

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

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

发布评论

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

评论(1

梦里的微风 2025-02-07 11:01:01

在您的应用程序配置中,执行以下操作:

[.NET 6 Program.cs]
builder.Services.Configure<WebEncoderOptions>(options =>
{
    options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
});

[Pre-.NET 6 Startup.cs]
services.Configure<WebEncoderOptions>(options =>
{
    options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
});

In your app configuration, do the following:

[.NET 6 Program.cs]
builder.Services.Configure<WebEncoderOptions>(options =>
{
    options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
});

Or

[Pre-.NET 6 Startup.cs]
services.Configure<WebEncoderOptions>(options =>
{
    options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文