Thursday, February 3, 2011

.NET requestValidationMode

I'm not sure how often other people have this kind of problem but when I decided to update my blog with "<" or ">" character, I have to find a HTML Encoder tool to encode the character. Since I have a window home server at home, I decided to write my own on-line HTML encoder page.

I have finished the web site and I host it on my home server. The web site is written by .NET 4.0 (I know it doesn't need to do with .NET..) and you can try it by clicking HERE

The syntax to encode the HTML text is really simple:

/* Comments*/
protected void Button1_Click(object sender, EventArgs e)
{
String value = "";
value = InputTxt.Text.Trim();
EncodedTxt.Text = "";
EncodedTxt.Text = Server.HtmlEncode(value).Trim();
}
To convert the text is really simple. However, the issue I had is about the HTML Request Validation. Once I have all the codes ready, I tried to run the page on my PC and test it by pasting "<header></header>". By clicking the button to trigger a page postback, I got the following error message:






After I did some research about this message, I figured out the error message I was having is because MS has a Request Validation to check the request contains any "Script" or not. For me, I have to disable the validation in order to perform the encoding. There are different ways to disable this validation. I choose to edit the web.config to disable the validation.

To disable the validation by editing the web.config file, open the web.confg file and update it like the following:
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime requestValidationMode="2.0" />
<pages validateRequest="false" />
</system.web>
After I updated the web.config file, I'm able to perform the encoding.

1 comment:

  1. Hi
    How can i go back to requestValidationMode="4.0" from requestValidationMode="2.0".I want the default features of the requestValidationMode in .NET 4.0.

    Thank you.

    ReplyDelete