Avoid the HttpRequestValidationException Exception

To avoid the HttpRequestValidationException Exception  you' ll have to set the flag validateRequest in the page directive .

See below:

<%@ Page language="c#" validateRequest="false" Codebehind="TestForm.aspx.cs" AutoEventWireup="false" Inherits="TestForm" %>

Ofcourse there is a downside. When you disable the validation you will have to prevent script injection yourself. Just don't forget to encode your html code and querystrings.

This can be done by using the following code snippet:

For HtmlCode

string safeFormFieldName = Server.HtmlEncode(formFieldName);

For QueryString

string safeQuery = Server.UrlEncode(fulfillmentFormName);

The .NET framework will escape the illegal characters like < ' >. It will replace the character < with &lt;

The browser will correctly interpret the &lt; and decode the character to <.

Published Fri, Jul 29 2005 5:55 PM by Chi Wai Man
Filed under:

Comments

# re: Avoid the HttpRequestValidationException Exception

<html>

Tuesday, October 28, 2008 4:24 PM by gfdgfdg

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 1 and 5 and type the answer here: