例外が発生したときに例外を表示するエラー メッセージ ページを作成しようとしています。エラー メッセージ ページには、例外が発生した前のページに戻るための戻るボタンがあります。
エラーページをリダイレクトするために使用されるコードは次のとおりです。
protected void btnAssign_Click(object sender, EventArgs e)
{
try
{
SqlDataSource3.Insert();
}
catch (Exception ex)
{
Session["Exception"] = ex;
Response.Redirect("~/ErrorMessage.aspx", false);
}
}
ここに私のglobal.asaxファイルのコードがあります
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception ex = Server.GetLastError().InnerException;
Session["Exception"] = ex;
Response.Redirect("~/ErrorMessage.aspx");
}
これが errorMessage ページのコードです。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Exception ex = (Exception)Session["Exception"];
Session.Remove("Exception");
Literal1.Text = "<p style='color:blue'><b>An unrecoverable error has occurred:</b></p><br /><p style='color:red'>" + ex.Message + "</p>";
}
}
protected void btnReturn_Click(object sender, EventArgs e)
{
Response.Redirect("~/IncidentAssignment.aspx");
}
割り当てボタンをクリックすると、errorMessage ページが開き、例外が表示されますが、戻るボタンをクリックすると、プログラムがクラッシュし、global.asax ファイルが指定され、blew に示すように、このコンテキストではセッション状態が利用できないと表示されます。
session["exception"] が Null である理由がわかりません。誰かが私の質問に答えてくれれば幸いです。ありがとう。