1

I a list of .aspx page which is in DropdownList that are from Application Root Directory. I am showing the Whole .aspx Page content in RichTextBox for Editing. But, When i click on Save Button it shows Error:A potentially dangerous Request.Form value was detected from the client (ctl00$MainContent$TextBox1="...xt" %>

Sample Code here:

<asp:TextBox ID="TextBox1" runat="server" Height="314px" TextMode="MultiLine" 
        Width="771px"></asp:TextBox>

<asp:Button ID="BtnSaveContent" runat="server" onclick="BtnSaveContent_Click" 
        Text="Save Content" />

In .aspx.cs file:
private void GetFilesNames()
    {


        TextReader tr = new StreamReader(Server.MapPath("") + "/CopyText.aspx");
        TextBox1.Text = tr.ReadToEnd();
        // close the stream
        tr.Close();
    }


    protected void BtnSaveContent_Click(object sender, EventArgs e)
    {
        WritetoFile();
    }

    private void WritetoFile()
    {
        TextWriter tw = new StreamWriter(Server.MapPath("") + "/CopyText.aspx");

        // write a line of text to the file
        tw.WriteLine(TextBox1.Text);

        // close the stream
        tw.Close();
    }

Page Header:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" validateRequest="false" CodeFile="CopyText.aspx.cs" Inherits="CopyText" %>

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" validateRequest = false
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

i have also tried validateRequest="false" at page level and control level. but doesn't work! Any Idea to resolve this error? Help appreciated!

SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143

1 Answers1

0

You're correct that you need validateRequest = false.

BUT if this is .Net 4.0 you also need to add requestValidationMode="2.0" to the httpRuntime configuration section of the web.config:

<system.web>
  <httpRuntime requestValidationMode="2.0"/>
</system.web>
Blachshma
  • 17,097
  • 4
  • 58
  • 72
  • Please show the header of your aspx page e.g. the `<%@ Page ` – Blachshma Dec 18 '12 at 12:14
  • now it doesn't throw error but changes to file doesnt persist. after closing browser it shows me "Page is being updated outside source do you want to reload?" when i click "yes" it still remains the previous page contents..! any reason? – SHEKHAR SHETE Dec 18 '12 at 12:19
  • WHAT shows you that message? The Visual Studio? and on what file? the Web.Config? – Blachshma Dec 18 '12 at 12:21
  • Yes VS2010 v4.0 framework! Message: "The file is modified outside source editor do you want to reload?" with two options YES/NO,YESTOALL/NOTOALL! After closing the browser. – SHEKHAR SHETE Dec 18 '12 at 12:30
  • I meant, where is this message shown? Inside Visual Studio or inside your browser? – Blachshma Dec 18 '12 at 12:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21329/discussion-between-shekhar-shete-and-blachshma) – SHEKHAR SHETE Dec 18 '12 at 12:36