简体   繁体   English

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

[英]500 internal server error on hidden __viewstate of asp.net page

I am trying to post the data to cross domain. 我正在尝试将数据发布到跨域。 It works fine if the form is not using runat="server" and its giving 500 internal error while posting when the form is using runat="server". 如果表单未使用runat =“ server”,并且在表单使用runat =“ server”进行发布时出现500内部错误,则此方法工作正常。

Upon debugging, I identified that the problem is with auto generated __viewstate code on the page. 调试后,我发现问题出在页面上自动生成的__viewstate代码。 Please find the below code. 请找到下面的代码。

Clientside HTML implementation: 客户端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 code: 服务器端代码:

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

        }
    }
}

Could you please help me in how to ignore the hidden viewstate code from client side or ignore the response viewstate in server side. 您能帮助我如何从客户端忽略隐藏的viewstate代码或如何在服务器端忽略响应viewstate。

Many Thanks Anna 非常感谢安娜

Try disabling view state by adding a line of code to your unload handler: 尝试通过向卸载处理程序添加一行代码来禁用视图状态:

Page.EnableViewState = false;

see this MSDN page 看到这个MSDN页面

I have found the solution for this problem. 我已经找到解决此问题的方法。 Please find below 如下请见

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

  }

}

Please find more info on this page : http://blogs.msdn.com/b/tom/archive/2008/03/14/validation-of-viewstate-mac-failed-error.aspx 请在此页面上找到更多信息: http : //blogs.msdn.com/b/tom/archive/2008/03/14/validation-of-viewstate-mac-failed-error.aspx

Many Thanks Anna 非常感谢安娜

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM