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

來源:互聯網
上載者:User

標籤:

 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()的用法和本質

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.