显示自定义错误页面问题
问:
我使用 ELMAH
- 错误记录模块和处理程序。
但我想向用户显示漂亮的错误页面。
我的问题有两个部分:
此中是否需要任何首选设计或信息 页面。(要求)。任何建议,链接都会很棒。
当我评论
Clear error
行时,错误页面会显示给用户 ,否则不会出现错误页面。为什么要清除 错误?以及如何显示错误页面。
protected void Application_Error(object sender, EventArgs e)
{
HttpContext ctx = HttpContext.Current;
Exception exception = ctx.Server.GetLastError();
string errorInfo =
"<br>Offending URL: " + ctx.Request.Url.ToString() +
"<br>Source: " + exception.Source +
"<br>Message: " + exception.Message +
"<br>Stack trace: " + exception.StackTrace;
ErrHandler.WriteError(errorInfo);
ctx.Server.ClearError();
}
<customErrors mode="On" defaultRedirect="Error.aspx"/>
Q:
I use ELMAH
- Error Logging Modules And Handlers.
but i wanna to show nice error page to the user .
My question has two parts:
Is there any preferred design or information required in this
page.(requirements) .any suggestions , links will be great.When i comment the
Clear error
line , The error page is shown to the user
, otherwise the error page doesn't appear.Why we clear the
errors?and How to show the error page .
protected void Application_Error(object sender, EventArgs e)
{
HttpContext ctx = HttpContext.Current;
Exception exception = ctx.Server.GetLastError();
string errorInfo =
"<br>Offending URL: " + ctx.Request.Url.ToString() +
"<br>Source: " + exception.Source +
"<br>Message: " + exception.Message +
"<br>Stack trace: " + exception.StackTrace;
ErrHandler.WriteError(errorInfo);
ctx.Server.ClearError();
}
<customErrors mode="On" defaultRedirect="Error.aspx"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试回答。
对于错误页面,我不使用 .aspx 页面,因为该页面经过 ASP .Net 模块,并且一个模块也可能有错误。所以我推荐一个静态页面(.html 页面),仅由 IIS 提供服务。当然,我也希望看到错误页面使用相同的网站主题,这可以使用母版页轻松制作,而且,也许当前会话中的其他信息,例如登录的用户等。所有这些都涉及与ASP .Net 因此也存在出现错误并被重定向到默认框架生成的错误页面的风险。因此,一个非常轻的错误页面(相同的颜色、字体样式等),可能带有一些图形和关于发生的情况的清晰消息对于用户来说就足够了。
对于 Elmah,我也使用了一些做法:首先,我不使用处理程序的“通常”名称,而是使用其他名称,例如“access-to-elmah-error.ashx”。想必大家都知道 elmah.ashx 或 error.ashx 用于显示服务器端错误以及对于黑客来说是一个很好的来源。接下来是我在授权部分保护它。如果网站出现严重问题并且我无法登录,我可以更改 web.config 以取消保护并查看发生了什么。
I'll try an answer.
For the error page I don't use an .aspx page because this page goes through ASP .Net modules and one module could have errors too. So I recommend a static page (.html one), served by IIS only. Sure, I would like me too to see that the error pages use the same website theme, which this is easily made with Master Pages, also, maybe other info from the current session, like logged on user, etc. All this involves interaction with the ASP .Net so it is a risk to get errors too and to be redirected to the default framework's generated error page. So a very light error page ( same colours, font styles, etc) with maybe some graphics and a clear message of what happened is very enough for the user.
For Elmah I'm using some practices too: first, I don't use the "usual" name for the handler, but something else, like "access-to-elmah-error.ashx". Think that everybody knows elmah.ashx or error.ashx is used to show the server side errors and what a good source for a hacker is. Next is that I protect it under the authorization section. If something is very wrong with the website and I can't login, I can change the web.config to un-protect it and see what happened.