当用户从我的 ASP.NET 应用程序下载文件时,会话会在他们下载文件几秒钟后过期。在可以执行任何任务的会话到期之前,但在大约 5-10 秒后,会话会重新启动并且它们会被注销。
我创建了一个简单的页面来演示这一点。要运行这个简单的页面,请创建一个新的 asp.net c# 项目,然后将代码插入一个新页面。
编辑:这似乎是一个 IE7 问题。Firefox 和 Chrome 不受影响。
我相信负责会话重启的代码是:
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=test.xml");
HttpContext.Current.Response.Write("<test>this is a test.</test>");
HttpContext.Current.Response.End();
要重现此问题:
- 将下面的代码复制到 asp.net 页面中。
- 使用 IE(我用的是 IE7,firefox 和 chrome 似乎没有这个问题)
- 请注意,会话是新的。
- 刷新页面; 请注意,会话不是新的。
- 下载文件并保存。
- 点击“刷新页面”按钮几次,直到重新显示“会话是新的”文本(大约 10 秒)。
以下是简单娱乐的代码:
<%@ Page Language="C#" AutoEventWireup="true" %>
<!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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<script runat="server">
private string sessionString {
get {
return HttpContext.Current.Session["sessionString"] == null ? null : HttpContext.Current.Session["sessionString"].ToString();
}
set {
HttpContext.Current.Session["sessionString"] = value;
}
}
protected void Page_Load(object sender, EventArgs e) {
Label1.Text = sessionString ?? "Session is null";
if(sessionString == null) {
Label1.Text = "Session is new";
Label1.BackColor = System.Drawing.Color.Red;
sessionString = "Session is now not null";
}
else {
Label1.Text = sessionString;
Label1.BackColor = System.Drawing.Color.White;
}
}
protected void LinkButton1_Click(object sender, EventArgs e) { }
protected void LinkButton2_Click(object sender, EventArgs e) {
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=test.xml");
HttpContext.Current.Response.Write("<test>this is a test.</test>");
HttpContext.Current.Response.End();
}
</script>
<form id="form1" runat="server">
<div>
<asp:Label id="Label1" runat="server" text="Label"></asp:Label> <br />
<asp:LinkButton id="LinkButton1" runat="server" onclick="LinkButton1_Click">Refresh Page</asp:LinkButton> <br />
<asp:LinkButton id="LinkButton2" runat="server" onclick="LinkButton2_Click">Download File</asp:LinkButton> <br /><br />
<b>Steps to recreate:</b>
<ol>
<li>Download the file and save it.</li>
<li>Hit the "Refresh Page" button a couple of times until the "Session is new" text is redisplayed.</li>
<li>Answer my question explaining what the heck is going on!</li>
</ol>
</div>
</form>
</body>
</html>