我正在尝试将数据发布到跨域。如果表单没有使用runat="server",并且在表单使用runat="server"时发布时出现内部错误500,则它可以正常工作。
经过调试,我发现问题出在页面上自动生成的__viewstate代码上。请查找以下代码。
客户端HTML实现:
<%@ Page Language="C#" CodeFile="Sample.aspx.cs" Inherits="Sample" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta charset="utf-8">
<title>Untitled Page</title>
<link href="FileUpload.css" rel="stylesheet" type="text/css" />
<script id="template-upload" type="text/x-tmpl">
</script>
<script id="template-download" type="text/x-tmpl">
</script>
<script src="../../test/FileUpload/jQueryv1.6.2.js"></script>
<script src="fileupload-js/jquery.ui.widget.js"></script>
<script src="fileupload-js/tmpl.min.js"></script>
<script src="fileupload-js/jquery.fileupload.js"></script>
<script src="fileupload-js/jquery.fileupload-ui.js"></script>
<script src="fileupload-js/locale.js"></script>
<script src="fileupload-js/main.js"></script>
</head>
<body>
<form id="fileupload" method="POST" runat="server">
<CCAB.Web:FileUpload runat="server"/>
</form>
</body>
</html>
服务器端代码:
public partial class SaveFile : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Response.AddHeader("Access-Control-Allow-Origin", "*")
if (Request.HttpMethod == "GET" || Request.HttpMethod == "HEAD")
{
Response.Write("GET Success");
}
else
{
for (int i = 0; i < Request.Files.Count; i++)
{
string filename = Request.Files[i].FileName;
Request.Files[i].SaveAs(@"\dev2\share$\Anna\test\" + filename);
Response.Write(filename);
Response.Write("Success");
}
}
}
}
你能帮助我如何忽略客户端隐藏的视图状态代码或忽略服务器端的响应视图状态吗。
非常感谢Anna
尝试通过向卸载处理程序添加一行代码来禁用视图状态:
Page.EnableViewState = false;
请参阅此MSDN页面
我已经找到了这个问题的解决方案。请在下面找到
public class BasePage : Page
{
private static string[] aspNetFormElements = new string[]
{
"__EVENTTARGET",
"__EVENTARGUMENT",
"__VIEWSTATE",
"__EVENTVALIDATION",
"__VIEWSTATEENCRYPTED",
};
protected override void Render(HtmlTextWriter writer)
{
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
base.Render(htmlWriter);
string html = stringWriter.ToString();
int formStart = html.IndexOf("<form");
int endForm = -1;
if (formStart >= 0)
endForm = html.IndexOf(">", formStart);
if (endForm >= 0)
{
StringBuilder viewStateBuilder = new StringBuilder();
foreach (string element in aspNetFormElements)
{
int startPoint = html.IndexOf("<input type="hidden" name="" + element + """);
if (startPoint >= 0 && startPoint > endForm)
{
int endPoint = html.IndexOf("/>", startPoint);
if (endPoint >= 0)
{
endPoint += 2;
string viewStateInput = html.Substring(startPoint, endPoint - startPoint);
html = html.Remove(startPoint, endPoint - startPoint);
viewStateBuilder.Append(viewStateInput).Append("rn");
}
}
}
if (viewStateBuilder.Length > 0)
{
viewStateBuilder.Insert(0, "rn");
html = html.Insert(endForm + 1, viewStateBuilder.ToString());
}
}
writer.Write(html);
}
}
请在此页面上查找更多信息:http://blogs.msdn.com/b/tom/archive/2008/03/14/validation-of-viewstate-mac-failed-error.aspx
非常感谢Anna