2011-08-29 26 views
1

我一直在阅读页面和应用程序级别的陷阱.Net错误,无法确定我想要做什么最好的套件。 我想要的只是一个基本的重定向到一个页面,告诉用户一个错误已经发生,无论错误或其页面发生了什么(还会有一些日志记录)。 这应该是应用程序级别吗?应用程序级别的.NET错误陷阱?

谢谢

+0

难道你不想记录这些错误,所以你有关于这些错误的细节? – Steven

回答

1

您可以将customErrors属性添加到web.config中。它会重定向到错误的指定页面:

<system.web> 
    <customErrors defaultRedirect="~/ErrorGeneric.html" mode="RemoteOnly"> 
     <error statusCode="500" redirect="~/Error500.html"/> 
     <error statusCode="404" redirect="~/Error404.html"/> 
    </customErrors> 
<system.web> 

此外,配置一个日志框架,可以帮助您存储错误信息以供日后分析。这里有几个框架:ELMAH,log4netCuttingEdge.Logging。我建议你使用这些框架中的一个,而不是在Application_Error事件中自行摆弄并编写日志功能。

+0

谢谢你们 - 那是我排序的 – DarkW1nter

+0

这和我的回答完全一样,只是我告诉你使用IIS管理控制台,因为它比直接在Web.config中编辑更简单,IIS管理控制台编辑web.config为你。 – 2011-08-29 13:40:57

0

请参阅Internet Information Server(IIS 7)中的.NET错误页面功能。在这里,您可以为不同的HTTP错误添加不同的错误页面。使用HTTP错误代码抛出HTTP错误代码,您需要为您的错误http://msdn.microsoft.com/en-us/library/bazc3hww.aspx

+0

会这样做,谢谢 – DarkW1nter

0

是的,您可以通过捕获Global.asax中的Application_Error事件来执行此操作。这是从MSDN的例子:

void Application_Error(object sender, EventArgs e) 
{ 
    // Get the exception object. 
    Exception exc = Server.GetLastError(); 

    // Handle HTTP errors 
    if (exc.GetType() == typeof(HttpException)) 
    { 
     //Redirect HTTP errors to HttpError page 
     Server.Transfer("HttpErrorPage.aspx"); 
    } 
    // For other kinds of errors give the user some information 
    // Log the exception and notify system operators 
    // Clear the error from the server 
    Server.ClearError(); 
} 

为完整的例子见this page,以及关于这个问题的一些一般性建议。