Disable request verification in ASP. NET MVC

Source: Internet
Author: User

By default, the ASP. net mvc framework prohibits users from submitting potentially dangerous content. For example, if HTML content is entered in a text input box, an error similar to the following is reported:

A potentially dangerous Request. Form value is detected from the client (TextBox1 = "<a> </a>.

This function can reduce the possibility of script attacks, but sometimes it is necessary to allow the user to input HTML content, such as publishing an article, and to disable the request verification function. At this time, write on the page:

ASPX code
<% @ Page ValidateRequest = "false" %>
This command is invalid and cannot be set in web. config. In MVC, if you want to disable request verification, you need the [ValidateInput] attribute. For example:

C # code
[ValidateInput (false)]
[AcceptVerbs (HttpVerbs. Post)]
Public ActionResult Create ([Bind (Exclude = "Id")] Product productToCreate)
{
If (! ModelState. IsValid)
Return View ();

_ DataModel. AddToProductSet (productToCreate );
_ DataModel. SaveChanges ();
Return RedirectToAction ("Index ");
}
 

 

Author: Meng xianhui

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.