2012-04-26 84 views
-1

包含Error頁面的網站中的每個頁面都是從Master頁面派生的。在主頁面我正在訪問會話變量。當我得到異常,在Page_Error或Application_Error事件中處理。從那裏我重定向到使用Server.Transfer的錯誤頁面,然後我在Error.aspx的母版頁中得到下面的異常。如果我使用Response.Redirect它正常工作。會話狀態只能在enableSessionState設置爲true時使用,當我使用Server.Transfer

Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>\<system.web>\<httpModules> section in the application configuration. 

請詳細解釋Server.Transfer的問題。

回答

0

當應用程序拋出異常時,我正在處理Application_Error事件。

protected void Application_Error(object sender, EventArgs e) 
    { 
     Exception ex = HttpContext.Current.Server.GetLastError(); 
     if (ex.Message == "File does not exist." && HttpContext.Current.Session == null) 
     { 
      if (((System.Web.HttpException)(ex)).GetHttpCode() == 404) 
      { 
       LogtheException(); 
      } 
     } 
     else 
     { 
      Log the Exception(Session["uname"].ToString()); 
      Server.Transfer(UrlMaker.ToError(ex.Message.ToString())); 
     } 
    } 

使用HttpContext.Current.Server.GetLastError();我得到最後一個例外。如果有任何異常,「文件不存在」。正在訪問會話變量。

首先,它會拋出相關應用程序異常,那麼如果任何CSS /映像文件的路徑是不正確的,然後緊接着就拋出了「文件不存在。」例外。例外是因爲沒有正確處理「文件不存在」的會話。案件。

Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>\<system.web>\<httpModules> section in the application configuration. 

現在我才知道,CSS和圖像請求通常不需要訪問會話,因此ASP不會話加載到內存中,你不能夠訪問它異常「文件不不存在「。

相關問題