模型属性忽略 html 实体的验证

发布于 2024-11-28 05:40:27 字数 459 浏览 4 评论 0原文

我想在数据库中输入html并将其显示为html。我这样编写视图模型:

public class TemplateVM
{
    [HiddenInput(DisplayValue = false)]
    public int TemplateId { get; set; }
    public string Name { get; set; }
    public string Content { get; set; }
}

属性 Content 应该能够接受 html。我该怎么做?现在,它会抛出以下错误:

从客户端检测到潜在危险的 Request.Form 值 (Content="

test

")。

我知道使用这是关于操作的,但我不希望它适用于每个属性。:

[ValidateInput(false)]

I want to input html in the database and also display it back as html. I wrote my view model like this:

public class TemplateVM
{
    [HiddenInput(DisplayValue = false)]
    public int TemplateId { get; set; }
    public string Name { get; set; }
    public string Content { get; set; }
}

the property Content should be able to accept html. How can I do this? Right now, it throws the error of:

A potentially dangerous Request.Form value was detected from the client (Content="<p>test</p>").

I'm aware of using this on the action, but I dont want it to apply to every property.:

[ValidateInput(false)]

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

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

发布评论

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

评论(2

短暂陪伴 2024-12-05 05:40:27

我建议您不要在整个模型上使用 ValidateInput 属性,而是在 Content 属性上使用 AllowHtml 属性:

public class TemplateVM
{
    [HiddenInput(DisplayValue = false)]
    public int TemplateId { get; set; }
    public string Name { get; set; }
    [AllowHtml]
    public string Content { get; set; }
}

此属性仅适用于 Content 属性,而其他属性仍在验证中。

Instead of using ValidateInput attribute on entire model, I suggest you use AllowHtml attribute on Content property:

public class TemplateVM
{
    [HiddenInput(DisplayValue = false)]
    public int TemplateId { get; set; }
    public string Name { get; set; }
    [AllowHtml]
    public string Content { get; set; }
}

This attribute is only applied for Content property, while other properties are still validated.

二智少女 2024-12-05 05:40:27

[ValidateInput(false)] 放在 TemplateVM 之上。它将适用于所有属性。

Put [ValidateInput(false)] on top of TemplateVM. It will apply to all properties.

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