context.Response.End()的用法和本質


 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 namespace Web_Cassini
 7 {
 8     /// <summary>
 9     /// response1 的摘要說明
10     /// </summary>
11     public class response1 : IHttpHandler
12     {
13 
14         public void ProcessRequest(HttpContext context)
15         {
16             context.Response.ContentType = "text/plain";
17             string username = context.Request["username"];
18             string password = context.Request["password"];
19             if(string.IsNullOrWhiteSpace(username))
20             {
21                 context.Response.Write("用戶名不能為空" + "\n");
22                 //return;
23                 try
24                 {
25                     context.Response.End(); //本質就是一個拋異常--> 效率低能不用最好不用
26                     //EndHandler(context);
27                 }
28                 catch(Exception ex)
29                 {
30                     context.Response.Write(ex.ToString());
31                 }
32             }
33             if (string.IsNullOrWhiteSpace(password))
34             {
35                 context.Response.Write("密碼不能為空"+"\n");
36                 //return;
37                 //context.Response.End(); 
38                 EndHandler(context);
39             }
40             context.Response.Write("插入成功" + "\n");
41         }
42 
43         private void EndHandler(HttpContext context)
44         {
45             context.Response.Write("終止HttpHandler" + "\n");
46             context.Response.End();  //子方法中終止進程 只有拋異常才能終止當前執行 不能用return End()本質就是拋出線程異常
47         }
48 
49         public bool IsReusable
50         {
51             get
52             {
53                 return false;
54             }
55         }
56     }
57 }
context.Response.End()的用法和本質

context.Response.End()的用法和本質:

用法:可以用來終止進程,即當前HttpHandler的執行,

  也可以在子方法中終止HttpHandler的執行,

  實際在子方法中終止程序,只有一種可能,那就是程序拋異常,所以context.Response.End()得本質就是拋出了線程異常

  由於拋異常的效率較低,所以能不能就不用,在主方法中最好還是使用return;終止程序比較好,

  但是在子方法中只能使用拋異常終止程序,即使用context.Response.End()終止程序。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM