標籤:
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 : IHttpHandler12 {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 try24 {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 IsReusable50 {51 get52 {53 return false;54 }55 }56 }57 }context.Response.End()的用法和本質
context.Response.End()的用法和本質:
用法:可以用來終止進程,即當前HttpHandler的執行,
也可以在子方法中終止HttpHandler的執行,
實際在子方法中終止程式,只有一種可能,那就是程式拋異常,所以context.Response.End()得本質就是拋出了線程異常
由於拋異常的效率較低,所以能不能就不用,在主方法中最好還是使用return;終止程式比較好,
但是在子方法中只能使用拋異常終止程式,即使用context.Response.End()終止程式。
context.Response.End()的用法和本質