วิธีการสร้างรายงานเพจใน ASP.NET โดยใช้ Visual .NET c# ข้อผิดพลาดแบบกำหนดเอง

หมายเลขบทความ (Article ID): 306355 - ผลิตภัณฑ์ที่เกี่ยวข้องในบทความนี้
ขยายทั้งหมด | ยุบทั้งหมด

เนื้อหาบนหน้านี้

สรุป

บทความนี้อธิบายวิธีการใช้รหัส Visual .NET c# trap และตอบสนองต่อข้อผิดพลาดเมื่อมันเกิดขึ้นใน ASP.NET asp.net ได้ทำการปรับปรุงข้อผิดพลาดในการจัดการตัวเลือกจากดั้งเดิม Microsoft Active Server หน้า (ASP) ใน ASP.NET คุณสามารถจัดการข้อผิดพลาดในระดับที่แตกต่างกันหลายในโปรแกรมประยุกต์ของคุณ

ลักษณะการทำงานใหม่ใน ASP.NET

asp.net เสนอการก้าวหลายในวิธีการจัดการ และตอบสนองต่อข้อผิดพลาด ใน ASP ดั้งเดิม คุณจัดการข้อผิดพลาดกับถัด "เมื่อข้อผิดพลาดการดำเนินการต่อไป" (หรือลอง-catchบล็อกใน JScript) อีกวิธีหนึ่งคือ ถ้าคุณกำลังเรียกใช้บริการข้อมูลทางอินเทอร์เน็ตของ Microsoft (IIS) 5.0 คุณใช้นั้นASPErrorวัตถุที่มีการสร้างแบบกำหนดเองรายงานข้อผิดพลาดหน้า อย่างไรก็ตาม approaches เหล่านี้มีข้อจำกัดของตนเอง

asp.net แสดงระดับต่าง ๆ ซึ่งคุณสามารถจัดการ และตอบสนองต่อข้อผิดพลาดที่อาจเกิดขึ้นเมื่อคุณเรียกใช้โปรแกรมประยุกต์ ASP.NET asp.net แสดงวิธีการสามหลักต่าง ๆ ที่ช่วยให้คุณ trap และตอบสนองต่อข้อผิดพลาดเมื่อมันเกิดขึ้น:Page_Error,Application_Errorและแฟ้มการกำหนดค่าแอพลิเคชัน (Web.config)

บทความนี้อธิบายวิธีการใช้คุณลักษณะใหม่เหล่านี้ใน ASP.NET ของแอพลิเคชัน แม้ว่าบทความนี้อธิบายถึงวิธีการที่แสดงข้อผิดพลาดที่กำหนดเองเพจและการรายงานข้อผิดพลาดทั่วไป ตามความเกี่ยวข้องโดยตรงกับ ASP.NET บทความนี้ไม่ได้อธิบายข้อผิดพลาดอื่น ๆ ในการจัดการ approaches เช่นนั้นลอง-catch-นำบล็อกและระบบข้อยกเว้นทั่วไปของภาษารันไทม์ (CLR)

วิธีการใช้วิธีการ Page_Error

กระบวนการPage_Errorตัวจัดการเหตุการณ์แสดงวิธีการข้อผิดพลาดของ trap ที่เกิดขึ้นในระดับของหน้า คุณสามารถจะแสดงรายละเอียดข้อผิดพลาด (ตามที่มีโค้ดตัวอย่างเป็นไปตาม), หรือคุณสามารถบันทึกเหตุการณ์ หรือทำการดำเนินการอื่น ๆ ได้

หมายเหตุ:ตัวอย่างนี้แสดงรายละเอียดข้อผิดพลาดของรายละเอียดในเบราว์เซอร์สำหรับวัตถุประสงค์ในการสาธิตเท่านั้น คุณจะต้องเป็น cautious เมื่อมีการให้แสดงข้อมูลรายละเอียดไปยังผู้ใช้ของโปรแกรมประยุกต์ โดยเฉพาะอย่างยิ่งเมื่อโปรแกรมประยุกต์ที่กำลังเรียกใช้บนอินเทอร์เน็ต การดำเนินการที่เหมาะสมมากจะเป็นการ แสดงข้อความให้กับผู้ใช้เหล่านั้นที่เกิดข้อผิดพลาดที่แจ้งเตือน และจากนั้น จริงบันทึกรายละเอียดข้อผิดพลาดที่ระบุในแฟ้มบันทึกเหตุการณ์

ตัวอย่างนี้ throws ยกเป็น null เว้น ซึ่งบังคับให้มีข้อผิดพลาดเกิดขึ้นในการPage_Loadตัวจัดการเหตุการณ์ ทำตามขั้นตอนเหล่านี้เพื่อสร้างหน้าเริ่มต้นที่จะสาธิตใช้นั้นPage_Errorตัวจัดการเหตุการณ์
  1. ทำตามขั้นตอนเหล่านี้เพื่อเพิ่มแฟ้มใหม่ที่ชื่อว่า PageEvent.aspx โครงการของคุณ:
    1. เปิด Microsoft Visual Studio .NET
    2. โซลูชัน Explorer คลิกขวาที่โหนโครงการ ชี้ไปที่addแล้ว คลิกเพิ่มฟอร์มเว็บ.
    3. ในการชื่อ:กล่องข้อความ ชนิดPageEvent.aspxแล้ว คลิกOPEN.
  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ค่าแอตทริบิวต์เท็จ. ไม่มีความแตกต่างสำคัญระหว่างค่าเริ่มต้นที่ใช้ ASP.NET และค่าเริ่มต้นที่รหัสต้นแบบของ Visual Studio .NET ที่กำหนดให้กับแอตทริบิวต์นี้ ถ้าการAutoEventWireupตั้งค่าแอตทริบิวต์เป็นเท็จไม่มีดำเนินการตัวจัดการเหตุการณ์ที่ประกาศในเพจ.ASPX ซึ่งอาจเป็นสับสนหากคุณไม่ทราบเกี่ยวกับฟังก์ชันการทำงานนี้
  3. จากนั้นแฟ้ม:เมนู คลิกบันทึก PageEvent.aspx.
  4. คลิกขวาหน้า และจากนั้น คลิกการดูในเบราว์เซอร์เมื่อต้องการเรียกใช้เพจนี้ ขอให้สังเกตว่า มีข้อผิดพลาด thrown และรายงานตามที่มีการระบุรหัส
หมายเหตุ:คุณอาจสังเกตเห็นว่า รหัสออกการเรียกServer.ClearError. ซึ่งทำให้ข้อผิดพลาดจากการดำเนินการต่อไปนี้Application_Errorตัวจัดการเหตุการณ์

นอกจากนี้ คุณควรจะจดหมายสืบทอดคุณลักษณะในการ@ หน้าdirective ถ้าสืบทอดกำลังกำหนดค่า คุณต้องสร้างโครงการก่อนที่คุณเรียกดูเพจนี้ ถ้าคุณไม่สร้างโครงการก่อน คุณได้รับข้อความแสดงข้อความแสดงข้อผิดพลาดต่อไปนี้:
'Project.PageEvent' ไม่ใช่ชนิดที่ถูกต้อง

วิธีการใช้วิธีการ Application_Error

เหมือนกับPage_Errorตัวจัดการเหตุการณ์ คุณสามารถใช้การApplication_Errorตัวจัดการเหตุการณ์ข้อผิดพลาดของ trap ที่เกิดขึ้นในโปรแกรมประยุกต์ของคุณ เนื่องจากการขอบเขตของทั้งแอพลิเคชันของเหตุการณ์ คุณสามารถบันทึกข้อมูลข้อผิดพลาดของโปรแกรมประยุกต์ หรือจัดการข้อผิดพลาดระดับโปรแกรมประยุกต์อื่น ๆ ที่อาจเกิดขึ้น

ตัวอย่างเป็นไปตามตาม precedingPage_Errorโค้ดตัวอย่าง และจะถูก fired ถ้าเกิดข้อผิดพลาดในPage_Loadไม่ได้ถูก trapped ในนั้นPage_Errorตัวจัดการเหตุการณ์ กระบวนการApplication_Errorมีระบุตัวจัดการเหตุการณ์ในแฟ้ม Global.asax ของโปรแกรมประยุกต์ของคุณ สำหรับซับซ้อน ขั้นตอนในส่วนนี้สร้างเพจใหม่ที่อยู่นอกกระบวนการข้อยกเว้น trap ข้อผิดพลาดในการApplication_Errorตัวจัดการเหตุการณ์ของแฟ้ม Global.asax และเขียนลงในบันทึกเหตุการณ์ข้อผิดพลาด ขั้นตอนต่อไปนี้แสดงให้เห็นถึงวิธีการใช้การ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>
    					

    หมายเหตุ:ข้อมูลที่กล่าวถึงใน "Page_Error"ส่วนที่เกี่ยวกับการAutoEventWireupแอตทริบิวต์ยังใช้กับตัวอย่างรหัสในขั้นตอนนี้ ดูรายละเอียดในการ "Page_Error"ส่วนสำหรับรายละเอียดเพิ่มเติม
  3. จากนั้นแฟ้ม:เมนู คลิกบันทึก AppEvent.aspx.
  4. เพิ่มApplication_Errorตัวจัดการเหตุการณ์แฟ้ม Global.asax ไป trap ข้อผิดพลาดที่คุณอยู่นอกกระบวนการในการPage_Loadตัวจัดการเหตุการณ์ของเพ AppEvent.aspx ขอให้สังเกตว่า คุณต้องเพิ่มอีกการใช้คำสั่งสำหรับการSystem.Diagnosticsnamespace เพื่อ 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. คลิกขวาหน้า และจากนั้น คลิกการดูในเบราว์เซอร์. In this case the page will be blank, however, you should notice that a new entry has been added in the event log. This sample makes an entry in the Application log, which is accessible from the Event Viewer. After logging the error you might want to redirect the user to another more user-friendly error page, or perform some additional actions if needed.

How to use the Web.config file

If you do not callServer.ClearErroror trap the error in thePage_ErrorหรือApplication_Errorevent handler, the error is handled based on the settings in the <customerrors> section of the Web.config file. In the <customerrors> section, you can specify a redirect page as a default error page (</customerrors></customerrors>defaultRedirect) or specify to a particular page based on the HTTP error code that is raised. You can use this method to customize the error message that the user receives.

If an error occurs that is not trapped at any of the previous levels in your application, this custom page is displayed. This section demonstrates how to modify the Global.asax file so thatServer.ClearErrorจะไม่เคยเรียก ด้วยเหตุ ข้อผิดพลาดถูกจัดการในแฟ้ม Web.config เป็นจุดสุดท้ายเพื่อ trap ข้อผิดพลาด
  1. เปิดแฟ้ม Global.asax จากตัวอย่างก่อนหน้านี้
  2. ข้อคิดเห็นออกแบบServer.ClearErrorบรรทัดเพื่อให้แน่ใจว่า มีข้อผิดพลาด surfaces ใน Web.config แฟ้ม
  3. Save your changes to Global.asax. Your code should now appear similar to the following:
    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. Add the following code to the <customerrors> section to redirect the user to a custom page: </customerrors>
    <customErrors defaultRedirect="http://hostName/applicationName/errorStatus.htm" mode="On">
    </customErrors>
    						
    หมายเหตุ:You must modify the file path indefaultRedirectattribute so that it references the relevant Web server and application names.
  5. Because the errors that are trapped at this level are sent to a default error page, you must create an error page named ErrorStatus.htm. Keep in mind that you are using this method to control what is presented to the user, so this example uses an .htm page for the error page. Add the following code to 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. To test the code, save the files, build the project, and then view AppEvent.aspx in the browser. 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 thedefaultRedirectแอตทริบิวต์ในส่วน <customerrors> คุณสามารถระบุหน้าเฉพาะการเปลี่ยนเส้นทางไปขึ้นอยู่กับรหัสข้อผิดพลาด HTTP ที่ถูกยกขึ้น องค์ประกอบลูก <error>อนุญาตสำหรับตัวเลือกนี้ ตัวอย่างเช่น: </error> </customerrors>
<customErrors defaultRedirect="http://hostName/applicationName/errorStatus.htm" mode="On">
	<error statusCode="404" redirect="filenotfound.htm" />
</customErrors>
				
หมายเหตุ:The page that is specified indefaultRedirectของ <customerrors>ส่วนไม่มีแฟ้ม.htm I</customerrors>

สังเกตว่า การ <customerrors>ส่วนประกอบด้วยคำ</customerrors>โหมดแอตทริบิวต์ที่ถูกกำหนดเป็นบน. กระบวนการโหมดแอตทริบิวต์ถูกใช้เพื่อควบคุมวิธีการเปลี่ยนเส้นทางข้อผิดพลาดเกิดขึ้น ตัวอย่างเช่น ถ้าคุณกำลังพัฒนาโปรแกรมประยุกต์ คุณมากที่สุดอาจต้องการดูข้อผิดพลาด ASP.NET ที่แท้จริง และไม่ต้องการจะเปลี่ยนเส้นทางไปยังเพจการข้อผิดพลาด user-friendly มากขึ้น กระบวนการโหมดแอตทริบิวต์รวมถึงการตั้งค่าต่อไปนี้:
  • บน: ข้อยกเว้น unhandled ผู้ใช้ที่เปลี่ยนเส้นทางไปที่ระบุdefaultRedirectหน้า This mode is used mainly in production.
  • ปิด: Users receive the exception information and are not redirected to thedefaultRedirectหน้า This mode is used mainly in development.
  • RemoteOnly: Only users who access the site on the local computer (by using localhost) receive the exception information. All other users are redirected to thedefaultRedirectหน้า This mode is used mainly for debugging.

การแก้ไขปัญหา

In its default installation on Microsoft Windows 2000 and on Microsoft Windows XP, ASP.NET runs Web application code in a worker process. The identity of this process defaults to an unprivileged local account called the ASPNET account. In beta releases of ASP.NET, the process identity was System, a powerful administrative account with many privileges on the machine.

In its default installation on Windows Server 2003 (IIS 6), ASP.NET runs Web application code in a worker process. The identity of this process defaults to a limited account called NetworkService.

For more information about this change and how it can effect running the code in this article, and other other code that might need additional access rights, visit the following Web sites:
Version 1 Security Changes for the Microsoft .NET Framework
http://msdn2.microsoft.com/en-us/library/ms994923.aspx

ข้อมูลอ้างอิง

For more information, visit the following Microsoft Web sites:
Exception Management in .NET
http://msdn2.microsoft.com/en-us/library/ms954599.aspxHttpServerUtility.ClearError Method
http://msdn2.microsoft.com/en-us/library/system.web.httpserverutility.clearerror(vs.71).aspxMSDN .NET Development Center
http://msdn2.microsoft.com/en-us/netframework/default.aspxMicrosoft .NET Home Page
http://www.microsoft.com/net/

คุณสมบัติ

หมายเลขบทความ (Article ID): 306355 - รีวิวครั้งสุดท้าย: 10 มกราคม 2554 - Revision: 4.0
ใช้กับ
  • Microsoft ASP.NET 1.0
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft ASP.NET 1.1
  • Microsoft Visual C# .NET 2003 Standard Edition
Keywords: 
kbconfig kbhowtomaster kbweb kbmt KB306355 KbMtth
แปลโดยคอมพิวเตอร์
ข้อมูลสำคัญ: บทความนี้แปลโดยซอฟต์แวร์การแปลด้วยคอมพิวเตอร์ของ Microsoft แทนที่จะเป็นนักแปลที่เป็นบุคคล Microsoft มีบทความที่แปลโดยนักแปลและบทความที่แปลด้วยคอมพิวเตอร์ เพื่อให้คุณสามารถเข้าถึงบทความทั้งหมดในฐานความรู้ของเรา ในภาษาของคุณเอง อย่างไรก็ตาม บทความที่แปลด้วยคอมพิวเตอร์นั้นอาจมีข้อบกพร่อง โดยอาจมีข้อผิดพลาดในคำศัพท์ รูปแบบการใช้ภาษาและไวยากรณ์ เช่นเดียวกับกรณีที่ชาวต่างชาติพูดผิดเมื่อพูดภาษาของคุณ Microsoft ไม่มีส่วนรับผิดชอบต่อความคลาดเคลื่อน ความผิดพลาดหรือความเสียหายที่เกิดจากการแปลเนื้อหาผิดพลาด หรือการใช้บทแปลของลูกค้า และ Microsoft มีการปรับปรุงซอฟต์แวร์การแปลด้วยคอมพิวเตอร์อยู่เป็นประจำ
ต่อไปนี้เป็นฉบับภาษาอังกฤษของบทความนี้:306355

ให้ข้อเสนอแนะ