如何使用c#编辑.aspx页面并在asp.net上单击按钮保存?
我从应用程序根目录下载了DropdownList中的.aspx页面列表。我在RichTextBox中显示整个.aspx页面内容以供编辑。但是,当我单击“保存”按钮时,它显示错误:从客户端检测到一个潜在危险的请求。表单值(ctl00$MainContent$TextBox1=“…xt”%> 此处的示例代码:如何使用c#编辑.aspx页面并在asp.net上单击按钮保存?,c#,asp.net,writetofile,textwriter,C#,Asp.net,Writetofile,Textwriter,我从应用程序根目录下载了DropdownList中的.aspx页面列表。我在RichTextBox中显示整个.aspx页面内容以供编辑。但是,当我单击“保存”按钮时,它显示错误:从客户端检测到一个潜在危险的请求。表单值(ctl00$MainContent$TextBox1=“…xt”%> 此处的示例代码: <asp:TextBox ID="TextBox1" runat="server" Height="314px" TextMode="MultiLine" Width=
<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();
}
在.aspx.cs文件中:
私有void GetFilesNames()
{
TextReader tr=新的StreamReader(Server.MapPath(“”+“/CopyText.aspx”);
TextBox1.Text=tr.ReadToEnd();
//关闭小溪
tr.Close();
}
受保护的无效BtnSaveContent_单击(对象发送者,事件参数e)
{
WritetoFile();
}
私有void WritetoFile()
{
TextWriter tw=新的StreamWriter(Server.MapPath(“”+“)/CopyText.aspx”);
//在文件中写入一行文本
tw.WriteLine(TextBox1.Text);
//关闭小溪
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>
请显示您的aspx页面的标题,例如,现在它不会抛出错误,但对文件的更改不会持续。关闭浏览器后,当我单击“是”时,它会显示“页面正在更新,是否要重新加载?”它仍然保留上一页的内容..!有什么原因吗?是什么向您显示了该消息?Visual Studio?在什么文件上?Web.Config?是VS2010 v4.0 framework!消息:“该文件在源代码编辑器外部被修改,是否要重新加载?”关闭浏览器后,有两个选项YES/NO,YESTOALL/NotAll!我的意思是,此消息显示在哪里?在Visual Studio中还是在浏览器中?