asp.net页面的隐藏__viewstate上有500个内部服务器错误

时间:2012-07-08 04:51:52

标签: c# .net client-server cross-domain

我正在尝试将数据发布到跨域。如果表单不使用runat =“server”并且在表单使用runat =“server”时发布时发出500内部错误,则它可以正常工作。

调试后,我发现问题出在页面上自动生成的__viewstate代码中。请找到以下代码。

Clientside 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>

Serverside代码:

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");
            }

        }
    }
}

请您帮我解决如何忽略客户端隐藏的视图状态代码或忽略服务器端的响应视图状态。

非常感谢 安娜

2 个答案:

答案 0 :(得分:1)

尝试通过向卸载处理程序添加一行代码来禁用视图状态:

Page.EnableViewState = false;

请参阅this MSDN page

答案 1 :(得分:0)

我找到了解决这个问题的方法。请在下面找到

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("\r\n");

          }

        }

      }



      if (viewStateBuilder.Length > 0)

      {

        viewStateBuilder.Insert(0, "\r\n");

        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

非常感谢 安娜