将HTTP响应保存到会话变量中

时间:2016-12-20 13:59:50

标签: c# html asp.net session-variables

我正在尝试将Http Reponse保存到Session变量中,但是我收到此错误

  

无法序列化会话状态。在“StateServer”和“SQLServer”模式下,ASP.NET将序列化会话状态对象,因此不允许使用不可序列化的对象或MarshalByRef对象。如果自定义会话状态存储在“自定义”模式下完成类似的序列化,则适用相同的限制。

private System.Web.HttpResponse _response;

Function1()
{
    Response.ContentType = "application/vnd.ms-excel";
    string attachment = "attachment; filename=Prac_Query_Report.xls";
    //Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    //Response.ContentType = "application/ms-excel";
    StringWriter stw = new StringWriter();
    HtmlTextWriter htextw = new HtmlTextWriter(stw);
    //ctl.RenderControl(htextw);
    _response = (System.Web.HttpResponse)Session["hi"];
    _response.Write(stw.ToString());
    FileInfo fi = new FileInfo(Server.MapPath("~/Stylesheets/Styles1.css"));
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    StreamReader sr = fi.OpenText();
    while (sr.Peek() >= 0)
    {
        sb.Append(sr.ReadLine());
    }
    sr.Close();
    Response.Write("<html><head><style type='text/css'>" + sb.ToString() + "</style>" + stw.ToString() + "</head></html>");
    stw = null;
    htextw = null;
}

Function2()
{
    Response.Write(footerBuilder.ToString());
    Session["hi"] = Response;
}

0 个答案:

没有答案