Global.ascx app_error 中的异常捕获
我在 asp.net 和 C# 中有 Web 应用程序,
我试图处理异常发生在该应用程序中的任何位置。
就像假设行为应该是如果发生这样的异常,
//generate your fictional exception
int x = 1;
int y = 0;
int z = x / y;
它应该在 global.ascx 文件的 app_error 中捕获它并将其重定向到 Default.aspx 页面。我得到了日志记录部分,但重定向不起作用,因为我仍然得到了
“/”应用程序中的服务器错误。 页。或者可能是它正在重定向并在中间被杀死..
这就是 global.ascx 中的内容,
protected void Application_Error(object sender, EventArgs e)
{
logger.Fatal(this.Server.GetLastError().GetBaseException());
logger.Info("FatalLogger Passed");
//get reference to the source of the exception chain
Exception ex = Server.GetLastError().GetBaseException();
Response.Redirect("~/Default.aspx?error=MessageHere");
}
这是 web.config 中的代码中的
<authentication mode="Forms">
<forms loginUrl="Login.aspx" defaultUrl="~/Default.aspx" name="GUI" slidingExpiration="true" timeout="30" path="/">
</forms>
</authentication>
任何想法。很乐意提供更多信息。
谢谢
,好的,我想要这种方法是有原因的,因为每当出现错误时,用户都会注销,我不希望发生这种情况,而是转到默认页面
I have web application in asp.net and C#
I am trying to handle exceptions if they occur anywhere within this application.
like suppose the behaviour should be if and exception like this occurs
//generate your fictional exception
int x = 1;
int y = 0;
int z = x / y;
it should catch it in the app_error of the global.ascx file and redirect it to the Default.aspx page. i got the logging part but the redirect is not working as i still get the
Server Error in '/' Application.
page. or may be it is redirecting and getting killed in the middle..
this is what is there in global.ascx
protected void Application_Error(object sender, EventArgs e)
{
logger.Fatal(this.Server.GetLastError().GetBaseException());
logger.Info("FatalLogger Passed");
//get reference to the source of the exception chain
Exception ex = Server.GetLastError().GetBaseException();
Response.Redirect("~/Default.aspx?error=MessageHere");
}
this in the code in web.config
<authentication mode="Forms">
<forms loginUrl="Login.aspx" defaultUrl="~/Default.aspx" name="GUI" slidingExpiration="true" timeout="30" path="/">
</forms>
</authentication>
any ideas.. ill; be happy to provide more information.
Thanks
ok i want this approach for a reason because whenever there is an error the user get logged out and i dont want that to happen instead go to the default page
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过在 Application_Error 中重定向之前调用 Server.ClearError() ?我已经有一段时间没玩这个了,但我相信如果你不调用 ClearError 那么框架仍然认为错误未处理。
Have you tried calling Server.ClearError() before the redirect in Application_Error? It's been a while since I played with this, but I believe that if you don't call ClearError then the framework still thinks the error is unhandled.
配置自定义错误页面
顺便说一句,我建议< a href="http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx" rel="nofollow">ELMAH 用于日志记录部分...
Configure custom error pages
BTW, I recommend ELMAH for the logging part...
尝试使用 Server.Transfer(page)
还要小心通过查询字符串传递错误消息,因为它可能会导致 XSS 问题。传递错误代码,然后根据代码显示消息(使用
switch
语句)Try using
Server.Transfer(page)
Also be wary of passing the error message via the Query String as it can open you up to XSS problems. Pass an error code and then display the message dependent on the code (using a
switch
statement)