在我的全局中,我有以下代码来处理错误发生时
//[..] code goes here
Server.Transfer("~/Error.aspx?ErrorID=" + errorId);
Run Code Online (Sandbox Code Playgroud)
它曾经是一个Response.Redirect完美的工作,除了它改变了网址(这就是为什么我要使用Server.Transfer)
不幸的是,现在当它尝试加载错误页面时,它在主页上尝试引用时会崩溃 Session
HttpException:
只有在配置文件或Page指令中将enableSessionState设置为true时,才能使用会话状态.还请确保System.Web.SessionStateModule或自定义会话状态模块包含在应用程序配置的\\部分中.
我的配置和页面都有enableSessionState.
我还发现了一些建议使用Context.RewritePath的链接 - 这只会导致为我加载空白页面.
使用Response.Redirect完美和按预期工作,所以我认为Server.Transfer是这里的问题.它是什么?
编辑代码:
protected void Application_Error(object sender, EventArgs e)
{
lock (_lockMe)
{
Exception ex = Server.GetLastError();
if (ex != null)
{
if (ex.InnerException != null)
ex = ex.InnerException;
ErrorLoggingManager.AddError(ex, new MembershipData(), ...); //etc
}
Server.ClearError();
//Some other database code for cleaning up some stuff when an error happens
}
try
{
if (Response != null) …Run Code Online (Sandbox Code Playgroud) 正如其他地方所建议的那样 ,我在自定义错误配置中使用redirectMode = ResponseRewrite,因此我的自定义错误页面可以访问异常信息.这有点像魅力一段时间了.
在添加更多"帮助用户从错误中恢复"类型功能时,我们需要一段先前存储在Session中的信息.实现这一点时,我发现当redirectMode = ResponseRewrite时,Session的各种途径以null结尾,但是当redirectMode = ResponseRedirect(或未定义)时,它们都被填充.
谁知道为什么?我们必须在具有异常信息(ResponseRewrite)或具有Session(ResponseRedirect)之间进行选择,这似乎很奇怪.
关于Rich Custom Error处理的MSDN文章告诉我,当控制传递方法是Server.Transfer时,Session才可用,这就是我假设在引擎盖下使用的ResponseRewrite.显然事实并非如此.
asp.net exception-handling web-config custom-errors redirectmode
在VS2010 SP1中调试我的MVC 4应用Application_Error()程序时,调用了处理程序但是Server.GetLastError()为null.
protected void Application_Error()
{
var exception = Server.GetLastError();
// exception is null. How is that possible?
}
Run Code Online (Sandbox Code Playgroud)
MSDN状态 Application_Error是
如果在...应用程序中的任何地方发生未处理的异常,则调用...您可以从该
GetLastError方法获取有关最新错误的信息.
Server.GetLastError()状态的MSDN文档
返回值:抛出的上一个异常.
我怎么可能处于Application_Error()被调用但Server.GetLastError()返回null的状态?
想知道你对这个解决方案的看法,如果这是将错误信息传递给自定义页面的正确方法吗?
在web.config中:
<customErrors mode="On" defaultRedirect="~/Error.aspx"></customErrors>
Run Code Online (Sandbox Code Playgroud)
在Global.asax中:
<script RunAt="server">
void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex != null && Session != null)
{
ex.Data.Add("ErrorTime", DateTime.Now);
ex.Data.Add("ErrorSession", Session.SessionID);
HttpContext.Current.Cache["LastError"] = ex;
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
在我的Error.aspx.cs中:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
if (HttpContext.Current.Cache["LastError"] != null)
{
Exception ex = (Exception)HttpContext.Current.Cache["LastError"];
if (ex.Data["ErrorTime"] != null && ex.Data["ErrorSession"] != null)
if ((DateTime)ex.Data["ErrorTime"] > DateTime.Now.AddSeconds(-30d) && ex.Data["ErrorSession"].ToString() == Session.SessionID)
Label1.Text = ex.InnerException.Message;
}
}
Run Code Online (Sandbox Code Playgroud)
问题:我不想从Global.asax做一个Server.Transfer因为..我不知道.对我来说似乎很笨拙.希望能够将customErrors更改为RemoteOnly.所以必须在某处保存最后一个异常,但不能是Session,所以保存到Cache但是有一些额外的数据(时间和SessionID),因为Cache是全局的,并且希望确保不向某人显示错误的错误.
我有点改变了我的代码.现在它只是: …
我正在运行 .NET 3.5 和 IIS7。
我试图使用 customErrors 重定向到一个仍然可以显示异常详细信息、堆栈跟踪等的自定义错误页面。我很难让它工作,尝试了我在网上找到的大约 20 种不同的方法(主要是在 stackoverflow 上),其中一些是其他人的轻微变化。我更喜欢在 Web.config 中进行重定向,因为我希望在代码之外轻松找到/编辑自定义错误页面。
这就是我最终开始工作的内容。我发帖是因为我尝试了很多在这里找到的更复杂的方法,但它们对我不起作用,我只想发布最终成功的简单方法。
网页配置:
<customErrors mode="RemoteOnly" defaultRedirect="~/Error.aspx" redirectMode="ResponseRewrite" />
Run Code Online (Sandbox Code Playgroud)
没有redirectMode="ResponseRewrite",我无法从我的自定义错误页面访问异常详细信息。
错误.aspx
protected void Page_Load(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex != null)
{
if (ex.GetBaseException() != null) ex = ex.GetBaseException();
litErrorMessage.Text = String.Format("<div class=\"error\">{0}</div>", ex.Message);
litErrorStackTrace.Text = String.Format("<b>Source:</b>\n{0}\n<b>Stack Trace:</b>\n{1}\n", ex.Source, ex.StackTrace);
}
else
{
litErrorStackTrace.Text = "No Exception information available.";
}
}
Run Code Online (Sandbox Code Playgroud)
我也尝试使用
HttpException ex = (HttpException)HttpContext.Current.Server.GetLastError();,如某些示例中所见,但这不起作用。
我还尝试了Global.asax -> Application_Error …
该网页是一个简单的注册类型页面.每个用户必须填写大约200个字段.有1个父表和6个子表.每个表信息都显示在单独的AJAX选项卡面板中,详细信息将保存在每个选项卡导航中.我已经在Intranet中托管了该网站,并且同时由100个用户输入了详细信息.最初一切都进展顺利,条目被保存在数据库中,但有一段时间后,AJAX加载图像被显示,网站似乎非常慢.用户的详细信息未保存在数据库中.最后Firefox浏览器崩溃了一些人,有些应用程序甚至没有注销.该网站被绞死了!我也重置了IIS,一切都持续了几分钟,同样的问题又开始了!我使用NHibernate框架,AJAX,ASP.Net 4.0作为前端,SQL server 2008作为后端.
以下是日志文件中的错误详细信息:
2014-08-11 10:05:40,953 [22] INFO ASP.global_asax servername : -
************************* 11/08/2014 10:05 **************************** IP Address: xx.x.xxx.xx Exception Type: System.Web.HttpException Exception: Request timed out. Source:
**************************************************************************
2014-08-11 10:05:40,984 [39] INFO ASP.global_asax servername : -
************************* 11/08/2014 10:05 ****************************`
IP Address: xx.x.xxx.xx
Exception Type: System.Web.HttpException
Exception: The client disconnected.
Source: System.Web
Stack Trace:
at System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError)
at System.Web.UI.ViewStateException.ThrowViewStateError(Exception inner, String persistedState)
at System.Web.UI.ClientScriptManager.EnsureEventValidationFieldLoaded()
at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument)
at System.Web.UI.Control.ValidateEvent(String uniqueID, String …Run Code Online (Sandbox Code Playgroud) asp.net ×5
c# ×2
web-config ×2
ajax ×1
asp.net-4.0 ×1
asp.net-mvc ×1
caching ×1
exception ×1
getlasterror ×1
redirectmode ×1
session ×1