2013-05-28 30 views
0

我想在實現IHttpModule的類中設置會話變量。我收到「未設置爲對象實例的對象引用」。在實現IHttpModule錯誤的類中設置會話變量

這裏是我的代碼:

public void Init(HttpApplication context) 
    { 
     context.PreRequestHandlerExecute += context_PreRequestHandlerExecute; 

    } 

    private void context_PreRequestHandlerExecute(object sender, EventArgs e) 
    { 
     HttpApplication app = (HttpApplication) sender; 
     HttpRequest request = app.Context.Request; 
     app.Session.Add("capath", request.QueryString["capath"].ToString()); 


    } 

請幫助。

+1

上調用.ToString()在以下不爲空? request.QueryString [「capath」] –

+0

檢查這個http://stackoverflow.com/questions/276355/can-i-access-session-state-from-an-httpmodule – Damith

+1

@MassimilianoPeluso omw,不能相信我錯過了所有這一切時間。謝謝。 –

回答

1

我猜問題是,查詢字符串不包含「capath」鍵/值,你會得到對象空例外,因爲你空對象