如何使用 c# 单击按钮时编辑.aspx页面和保存在 asp.net 中



我列出了来自应用程序根目录的下拉列表中.aspx页面。我正在富文本框中显示整个.aspx页面内容以进行编辑。但是,当我单击"保存"按钮时,它显示错误:从客户端检测到具有潜在危险的 Request.Form 值(ctl00$MainContent$TextBox1="...xt" %>

示例代码在这里:

<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 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" %>

我也在页面级别和控制级别尝试了 validateRequest="false"。 但不起作用!有什么想法可以解决此错误吗?感谢帮助!

你是对的,你需要validateRequest = false .

但是,如果这是.Net 4.0,您还需要将requestValidationMode="2.0"添加到web.config的httpRuntime配置部分:

<system.web>
  <httpRuntime requestValidationMode="2.0"/>
</system.web>

最新更新