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

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

问题描述

我怎样才能让用户输入到HTML使用ASP.net MVC的特定字段。

我有一个长格式与被映射到控制器中的这个复杂的对象很多领域。

我想提出一个字段(说明)允许HTML,我将在稍后preform我自己的环境卫生上。


解决方案

添加以下的属性要允许HTML控制器的动作(后):

  [ValidateInput(假)]

编辑:按照 Charlino 评论:

在你的web.config设置使用的验证模式。见<一href=\"http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.requestvalidationmode.aspx\">MSDN:

 &LT;的httpRuntime requestValidationMode =2.0/&GT;

修改2014年9月:按照 sprinter252 评论:

您现在应该使用 [AllowHtml] 属性。从 MSDN 参见下文:


  

有关ASP.NET MVC 3应用程序,当您需要发布HTML回
  你的模型,不使用ValidateInput(假)关闭请求
  验证。只需添加[AllowHtml]你的模型属性,像这样:

 公共类BlogEntry {
    公众诠释用户ID {获取;设置;}
    [AllowHtml]
    公共字符串BlogText {获取;设置;}
 }


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.

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

解决方案

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

[ValidateInput(false)] 

Edit: As per Charlino comments:

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

<httpRuntime requestValidationMode="2.0" />

Edit Sept 2014: As per sprinter252 comments:

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

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;}
 }

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

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