2015-05-29 30 views
1

我有以下一段代碼,它引發了一條錯誤消息,我需要在web.config上啓用會話,但目前爲止我沒有發現任何事情,所以任何人都可以告訴我它是如何工作的?Session.Abandon未觸發或投擲錯誤消息

我已經試過也對的Page_Load衰落中的代碼,但服務器似乎避免了會話的指示:

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

回答

0

運行我有同樣的問題,這是我做過什麼 在web.config中只添加這

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

,並在你的頁面,你正在使用

EnableSessionState="True" 

,你可以嘗試刪除一個會話,並檢查它是否已被刪除像這樣

Session.Remove("test"); 
if (Session["test"] == null) 
{ 
//woooo it work ; 
} 
else 
{ 
// what ???? 
}