IHttpModuleを拡張するASP.NETアプリケーションとdllがあります。以下の方法を使用して、セッション変数をhttpcontextに保存しました。
public class Handler : IHttpModule,IRequiresSessionState
{
public void Init(HttpApplication httpApp)
{
httpApp.PreRequestHandlerExecute += new EventHandler(PreRequestHandlerExecute);
}
public void PreRequestHandlerExecute(object sender, EventArgs e)
{
var context = ((HttpApplication)sender).Context;
context.Session["myvariable"] = "Gowtham";
}
}
asp.net Default.aspxページで、コードを使用して値を取得しました。
public partial class _Default : System.Web.UI.Page, IRequiresSessionState
{
protected void Page_Load(object sender, EventArgs e)
{
String token = Context.Session["myvariable"].ToString();
}
}
エラー応答が発生します
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
変数がセッションに格納されているかどうかを確認するために、セッションに値を格納した後、クラスハンドラーのメソッドに従ってチェックをクロスしました。
string ss = context.Session["myvariable"].ToString();
それはうまく実行され、セッションから値を取得しました。