???? ID: 306355 - ????? ???????: 29 ??????? 2010 - ??????: 4.0

????? ????? C# .NET ?? ????? ???? asp.NET ??? ??????? ?? ?????????? ?????? ????? ?? ??? ???? ????

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

?? ???? ????? ???? ?? ?? trap ?? ?? ?? asp.NET ??? ??, ?? ????????? ?? ??? ???????? ???? ?? ??? ????? C# .NET ??? ?? ????? ???? ????? ASP.NET ?? ????? ?????? ?? ???????? Microsoft ?????? ????? ????? (ASP) ???????? ??? ?????? ??? ASP.NET, ??? ?? ???? ????? ?????? ?? ????????? ?? ???? ??????????? ??? ????? ?? ???? ????

ASP.NET ??? ?? ????????

ASP.NET ???? ?? ?? ???? ??? ????? ?? ????????? ?? ??? ???????? ??? ?? advances ???????? ???? ??? ???????? ASP ??? ?? ????? "?? ?????? ???? ???? ????" ???? ????????? (??catch-?? ?????? ??????????? JScript ???)? Alternately, ??? ?? Microsoft ??????? ????? ?????? (IIS) 5.0 ??? ??? ???, ?? ?? ????? ????ASPError????? ?????????? ??? ????? ?????? ????? ?? ??? ???????? ??? ???????, ?? approaches ???? ?????? ????

ASP.NET ??? ?? ?? ?? ???? ??? ????? ?? ???????? ????????? ?? ?? ?? ?? ???? ?? ?? ?? ???? asp.NET ????????? ?? ????? ?? ??? ?? ?????? ?? ?????? ????? ??? ASP.NET ?? ??? ????? ??????? ?? trap ?? ?? ?? ??, ?? ????????? ?? ??? ???????? ???? ?? ??? ???? ?????? ???? ??:Page_Error,Application_Error, ?? ????????? ??????????? ????? (Web.config)?

????????? ???? ?? ?? ???? ?? ???? ???? asp.NET ??? ?? ?? ???????? ?? ????? ???? ?? ??? ?????????? Although this article describes how to provide custom error pages and general error reporting as it relates directly to ASP.NET, this article does not describe other error handling approaches such as thetry-catch-finallyblock and the Common Language Runtime (CLR) exception system.

How to use the Page_Error method

ThePage_Errorevent handler provides a way to trap errors that occur at the page level. You can simply display error information (as the sample code to follow does), or you can log the event or perform some other action.

???:?? ?????? ??? ???? ???????? ?????????? ?? ??? ???????? ??????? ?????? ??????? ????????? ???? ??? ?? ????????? ?? ??????? ??? ????? ?? ??? ??????? ????????? ??????? ?? ??????? ?? ????????? ?? ??? ?? ?? cautious ???? ????? ?????? ??? ?????? ? ?? ?? ?? ?????? ????? ????, ?? ??? ?????? ??? ?????? ??????? ?????? ????? ????? ??? ??? ?????????? ?? ??? ??? ????? ????????? ???? ?? ??? ?? ??????? ???? ?????? ?? ???? ???

?? ?????? ??? ?? ?? ?????, ?? ???? ?????? ??? ???? ?? ??? forces throwsPage_Load????? ??????? ????????? ????? ?? ????? ???? ??? demonstrate ???? ?? ????? ?? ???, ????? ????? ?? ???? ????Page_Error????? ???????
  1. ???? ????????? ??? PageEvent.aspx ??? ?? ??? ?? ????? ?????? ?? ???, ????? ????? ?? ???? ????:
    1. Microsoft Visual Studio .NET ??????
    2. ?????? Explorer ???, ????????? ??? ?? ????-????? ????, ?? ????? ????add?? ????-????? ????, ?? ???? ?????? ??????? ?????.
    3. ??????????? ????? ???, ??????PageEvent.aspx?? ????-????? ????, ?? ???? ????????.
  2. ????? ??? PageEvent.aspx ??? ??????:
    <script language=C# runat="server">
    void Page_Load(object sender, System.EventArgs e)
    {
    	throw(new ArgumentNullException());
    }
    
    public void Page_Error(object sender,EventArgs e)
    {
    	Exception objErr = Server.GetLastError().GetBaseException();
    	string err =	"<b>Error Caught in Page_Error event</b><hr><br>" + 
    			"<br><b>Error in: </b>" + Request.Url.ToString() +
    			"<br><b>Error Message: </b>" + objErr.Message.ToString()+
    			"<br><b>Stack Trace:</b><br>" + 
    	                  objErr.StackTrace.ToString();
    	Response.Write(err.ToString());
    	Server.ClearError();
    }
    </script> 
    					

    ???:?? ??? ????? ???AutoEventWireup??????? ?? ?????? ??? ?? ??? ???? ??? ??? ?? ???? ?????? ??? ?? ????? ???? ?? ??? ??? ???AutoEventWireup???????, ??????? ???True????? ??? ??? ??? ?? Visual Studio .NET ?? ????? ???? ??????????? ?? ?????? ???? ?? ??? ???, ??? ??????? ???????? ??? ?? ?????? ??? ?? ???? ??AutoEventWireup??????? ??? ?? ???FALSE. ??????? ??? asp.NET ?? ????? ???? ?? ?? ?? ??????? ?? ??? Visual Studio .NET ???????? ??? ???? ?? ?? ??????? ??? ?? ??? ?? ?????????? ???? ??? ???AutoEventWireup??????? ??? ??? ???? ?? ??? ??FALSE, .ASPX ????? ??? declared ????? handlers ???? ????? ?? ??? ?? ?? ??????????? ?? ???? ??? ???? ????? ???, ?? confusing ?? ???? ???
  3. ?????????? ??,PageEvent.aspx ??????.
  4. ????? ?? ????-????? ????, ?? ???? ?????????? ??? ?????????? ?? ?????? ????? ??? ?? ?????? thrown ?? ??? ??????????? ?? ?????? ?? ??????? ???? ???
???:???? ??? issues ??? ???? ?? ??? ????? ?? ???? ??Server.ClearError. ?? ???? ?? ??? ???? ???? ?? ?????? ????? ??Application_Error????? ???????

???? ??? ??, ???? ????? ?? ???? ?? ???Inherits??? ???????@ ?????directive. ???Inherits???, ????? ?? ??? ??????? ???? ?? ???? ????????? ??????? ???? ?????? ??? ??? ?? ???? ??? ????????? ????, ???? ????? ?????? ????? ??????? ???? ??:
'Project.PageEvent' ??? ????? ?????? ???? ??

Application_Error ???? ?? ????? ???? ????

?? ????Page_Errorevent handler, you can use theApplication_Errorevent handler to trap errors that occur in your application. Due to the event's application-wide scope, you can log of application error information or handle other application-level errors that may occur.

The sample to follow is based on the precedingPage_Errorcode sample and would be fired if the error inPage_Loadwas not trapped in thePage_Error????? ??????? TheApplication_Errorevent handler is specified in the Global.asax file of your application. Simplicity, ?? ??? ?? ??? ??? ??? ????? ????? ?????? ????? throw, ??? ?????? trap ???? ?? ??? ??? ??? ?????Application_Error????? ?????? Global.asax ????? ?? ?????? ????? ??? ???? ?? ??? ???? ?? ??? ????? ????? ?? ???? demonstrate ?? ????? ???? ????Application_Error????:
  1. ???? ????????? ??? AppEvent.aspx ??? ?? ??? ?? ????? ???????
  2. ????? ??? AppEvent.aspx ??? ??????:
    <script language=C# runat="server">
    	void Page_Load(object sender, System.EventArgs e)
    	{
    		throw(new ArgumentNullException());
    	}
    </script>
    					

    ???:??????? ??? discussed "Page_Error"?? ???? ??? ??????AutoEventWireup??????? ?? ??? ??? ??? ????? ?? ??? ?? ???? ????? ??????? ??? "Page_Error"???? ??????? ?? ??? ????
  3. ?????????? ??,AppEvent.aspx ??????.
  4. ?????Application_Error????? ?????? Global.asax ????? ?????? trap ???? ?? ??? ?? throw ???? ?? ???Page_Load????? ?????? AppEvent.aspx ????? ?? ??? ????? ?? ?? ?? ?????? ?????? ??????? ????? ?????? ??? ???????? ???System.Diagnostics????? ??? ?? ????? ???? ?? ??? Global.asax ???? ?? ??? ??? ????? ???

    Global.asax ????? ??? ????? ??? ??????:
    using System.Diagnostics;
    
    protected void Application_Error(object sender, EventArgs e)
    {
    	Exception objErr = Server.GetLastError().GetBaseException();
    	string err =	"Error Caught in Application_Error event\n" +
    			"Error in: " + Request.Url.ToString() +
    			"\nError Message:" + objErr.Message.ToString()+ 
    			"\nStack Trace:" + objErr.StackTrace.ToString();
    	EventLog.WriteEntry("Sample_WebApp",err,EventLogEntryType.Error);
    	Server.ClearError();
    	//additional actions...
    } 
    					
  5. Global.asax ????? ???????
  6. Visual Studio .NET, ??? ??????????? ??,?????.
  7. ????? ?? ????-????? ????, ?? ???? ?????????? ??? ?????. ?? ?????? ??? ????? ????? ?? ?????, ??? ??, ???? ????? ????? ?? ?? ????????? ????? ??? ??? ????? ??? ??? ?? ????? ??? ????????? ???, ????? ?????? ?? ?????? ?? ???? ?? ?? ???? ????????? ????? ??? ?????? ??? ???? ?? ??? ?? ?????????? ?? ???? ???? ???? ???????? ?????? ????? ?? ?????????????? ????, ?? ??? ?????? ?? ?? ??? ???????? ???????? ???? ?? ??? ????? ?? ???? ???

Web.config ????? ?? ????? ???? ????

??? ?? ??? ????Server.ClearError?????? ??? trap ??Page_Error??,Application_Error????? ??????, ?????? Web.config ????? ?? <customerrors>??? ??? ???????? ?? ?????? ??????? ??? ??? <customerrors>??? ???, ??? ???????? ?????? ????? ?? ??? ??? ???? ???????????? ????? ????????? ?? ???? ??? (</customerrors></customerrors>defaultRedirect) ?? ??? ????? ??? ????? ?? HTTP ?????? ??? ?? ?????? ???? ?? ??? ????????? ????? ?????????? ?? ????? ???? ?????? ????? ?? ???????? ???? ?? ??? ?? ?? ???? ?? ????? ?? ???? ????

??? ??? ?????? ???? ?? ?? ???? ?? ????????? ?? ???? ??? ????? ?????? ?? trapped ???? ??, ?? ????? ????? ????????? ???? ??? This section demonstrates how to modify the Global.asax file so thatServer.ClearError?? ??? ???? ??? ????????????, ?????? ?? ????? Web.config ???? ??? ?????? trap ???? ?? ??? ????? ????? ?? ??? ????
  1. ????? ?????? ?? Global.asax ????? ?? ??????
  2. ???? ???????Server.ClearError????????? ???? ?? ?????? Web.config ??? surfaces ???? ?? ??? ?????? ??????
  3. Global.asax ???? ?? ??? ???? ???????? ??????? ???? ??? ????? ?? ???? ?? ????? ???? ?????:
    using System.Diagnostics;
    
    protected void Application_Error(object sender, EventArgs e)
    {
    	Exception objErr = Server.GetLastError().GetBaseException();
    	string err =	"Error Caught in Application_Error event\n" +
    			"Error in: " + Request.Url.ToString() +
    			"\nError Message:" + objErr.Message.ToString() + 
    			"\nStack Trace:" + objErr.StackTrace.ToString();
    	EventLog.WriteEntry("Sample_WebApp",err,EventLogEntryType.Error);
    	//Server.ClearError();
    	//additional actions...
    } 
    					
  4. ????? ??? ?????? <customerrors>?????????? ?? ?? ????? ????? ?? ??? ?????????????? ???? ?? ??? ??????:</customerrors>
    <customErrors defaultRedirect="http://hostName/applicationName/errorStatus.htm" mode="On">
    </customErrors>
    						
    ???:????? ?? ??? ?????? ???? ????defaultRedirect????? ?? it ??????? ??? ????? ?? ????????? ??? ?? ?????? ??? ????????
  5. ?? ???? ?? trapped ????????? ???? ?? ??? ??? ??????? ?????? ????? ???? ???, ??????? ?? ErrorStatus.htm ???? ?????? ????? ????? ????? ?? ???? ?? ???????? ???? ?? ??? ?????????? ?? ?????? ?????? ????? ?? ??? ???. htm ????? ?? ????? ???? ?? ???? ?? ????????? ???? ?? ??? ?? ?????? ????? ?? ??? ??? ?? ????? ??? ????? ????? ??? ErrorStatus.htm ??? ??????:
    <HTML>
    <HEAD>
    <TITLE></TITLE>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 7.0">
    </HEAD>
    <BODY>
         <b>Custom Error page!</b>
         <br>
         You have been redirected here from the <customErrors> section of the 
         Web.config file.
    </BODY>
    </HTML>
    					
  6. ??? ?? ??????? ?? ??? ?????? ?? ??????, ????????? ?????, ?? ?? AppEvent.aspx ??????? ??? ?????? Notice that when the error is thrown, you are redirected to the ErrorStatus.htm page.
Although you can reference a default error page in the value of thedefaultRedirectattribute in the <customerrors> section, you can also specify a particular page to redirect to based on the HTTP error code that is raised. The <error> child element allows for this option. For example: </error></customerrors>
<customErrors defaultRedirect="http://hostName/applicationName/errorStatus.htm" mode="On">
	<error statusCode="404" redirect="filenotfound.htm" />
</customErrors>
				
???:The page that is specified indefaultRedirectof the <customerrors> section is an .htm file. I</customerrors>

Notice that the <customerrors> section includes a </customerrors>modeattribute that is set toOn. Themodeattribute is used to control how the error redirection occurs. For example, if you are developing the application, you most likely want to see the actual ASP.NET error messages and do not want to be redirected to the more user-friendly error page. Themodeattribute includes the following settings:
  • On: Unhandled exceptions redirect the user to the specifieddefaultRedirectpage. This mode is used mainly in production.
  • Off: Users receive the exception information and are not redirected to thedefaultRedirectpage. This mode is used mainly in development.
  • RemoteOnly: ???? ??????? ???????? ?? ???? (?? ??????? ????? ?? ????? ????) ??? ?????? ???? ???? ???????????? ?? ????? ??????? ??????? ???? ??? ???? ??? ???????????? ?? ??? ?????????????? ??defaultRedirect?????? ?? ??? mainly ??????? ?? ??? ????? ???? ???? ???

?????? ??????

???? ??????? ??????? ??? Microsoft Windows 2000 ?? Microsoft Windows XP ??, ASP.NET ???? ????? ????????? ??? ??? ????????? ??? ????? ???? ??? ?? ????????? ?? ????? defaults ASPNET ???? ???? ?? unprivileged ??????? ???? ?? ??? ??? ASP.NET ?? ???? ?????????, ??? ????????? ????? ??????, ?? ????? ???????????? ???? ?? ????????????? ?? ??? ?? ???? ?? ???? ??? ???

???? ??????? ??????? ??? Windows Server 2003 (IIS 6) ??, ASP.NET ???? ????? ????????? ??? ??? ????????? ??? ????? ???? ??? ?? ????????? ?? ????? defaults ???? ????? ???? NetworkService ??? ???? ???

?? ???????? ?? ???? ?? ?? ???? ??? ?????? ?? ???? ??? ???, ?? ???? ????? ??? ???????? ????? ?????? ?? ???????? ?? ???? ?? ?? ?? ??? ?? ???? ??? ???? ??????? ?? ??? ????? ??? ?????? ?? ????:
??????? 1 ??????? ???????? Microsoft .NET Framework ?? ???
HTTP://msdn2.Microsoft.com/en-us/library/ms994923.aspx (http://msdn2.microsoft.com/en-us/library/ms994923.aspx)

??????

???? ??????? ?? ???, ????? Microsoft ??? ?????? ?? ????::
.NET ??? ????? ???????
HTTP://msdn2.Microsoft.com/en-us/library/ms954599.aspx (http://msdn2.microsoft.com/en-us/library/ms954599.aspx) HttpServerUtility.ClearError ????
(vs.71) http://msdn2.Microsoft.com/en-us/library/SYSTEM.Web.httpserverutility.clearerror .aspx (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebHttpServerUtilityClassClearErrorTopic.asp) MSDN .NET ????????? ?????
HTTP://msdn2.Microsoft.com/en-us/netframework/default.aspx (http://msdn2.microsoft.com/en-us/netframework/default.aspx) Microsoft .NET ??? ?????
http://www.microsoft.com/net/ (http://www.microsoft.com/net/)

???? ???? ???? ??:
  • Microsoft ASP.NET 1.0
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft ASP.NET 1.1
  • Microsoft Visual C# .NET 2003 Standard Edition
??????: 
kbconfig kbhowtomaster kbweb kbmt KB306355 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:306355  (http://support.microsoft.com/kb/306355/en-us/ )