【发布时间】:2015-05-30 00:30:03
【问题描述】:
我有以下代码抛出一条错误消息,我需要在 web.config 上启用 Session,但到目前为止我没有找到任何东西,所以谁能告诉我它是如何工作的?.
我也尝试将代码放在 Page_Load 上的 Decline 中,但服务器似乎避免了 Session 的指令:
public partial class Decline : DataPage
{
protected Decline() {
Session.RemoveAll();
Session.Clear();
Session.Abandon();
Response.Cookies.Remove("Accessed");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("~/Account/Login.aspx");
}
}
这是完整的消息:
会话状态只能在 enableSessionState 设置为 true 时使用,无论是在配置文件中还是在 Page 指令中。还请确保 System.Web.SessionStateModule 或自定义会话状态模块包含在应用程序配置的
<configuration>\<system.web>\<httpModules>部分中
这是运行 .net 4 框架,来自以下答案的配置不太适合我当前的项目。因为那提供了一个 .net mvc 2 解决方案。因此,如果在 .net 4 上有一个等价物,那将会很有帮助
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
Session state can only be used when enableSessionState is set to true either in a configuration
另外,我已经为我的页面提供了以下行
<%@ Page Title="" Language="C#" CodeFile="Decline.aspx.cs" Inherits="Decline" enableSessionState="true" %>
无论它是否存在,都没有任何区别。也在 IIS 7.5 上运行
【问题讨论】: