允许用户在 ASP.NET MVC 中输入 HTML - ValidateInput 或 AllowHtml [英] Allow User to input HTML in ASP.NET MVC - ValidateInput or AllowHtml

查看:23
本文介绍了允许用户在 ASP.NET MVC 中输入 HTML - ValidateInput 或 AllowHtml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何允许用户使用 ASP.net MVC 将 HTML 输入到特定字段中.

How can I allow a user to input HTML into a particular field using ASP.net MVC.

我有一个很长的表单,其中包含许多映射到控制器中这个复杂对象的字段.

I have a long form with many fields that get mapped to this complex object in the controller.

我想让一个字段(描述)允许 HTML,我将在稍后执行我自己的卫生.

I would like to make one field (the description) allow HTML which I will preform my own sanitation on at a later point.

推荐答案

在你想要允许 HTML 的控制器中添加以下属性(post):

Add the following attribute the action (post) in the controller that you want to allow HTML for:

[ValidateInput(false)] 

根据 Charlino 评论:

在您的 web.config 中设置使用的验证模式.请参阅 MSDN:

In your web.config set the validation mode used. See MSDN:

<httpRuntime requestValidationMode="2.0" />

2014 年 9 月根据 sprinter252 评论:

您现在应该使用 [AllowHtml] 属性.请参阅以下 MSDN:

You should now use the [AllowHtml] attribute. See below from MSDN:

对于 ASP.NET MVC 3 应用程序,当您需要将 HTML 回传到你的模型,不要使用 ValidateInput(false) 来关闭请求验证.只需将 [AllowHtml] 添加到您的模型属性中,如下所示:

For ASP.NET MVC 3 applications, when you need to post HTML back to your model, don’t use ValidateInput(false) to turn off Request Validation. Simply add [AllowHtml] to your model property, like so:

public class BlogEntry {
    public int UserId {get;set;}
    [AllowHtml] 
    public string BlogText {get;set;}
 }

这篇关于允许用户在 ASP.NET MVC 中输入 HTML - ValidateInput 或 AllowHtml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆