using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Web_Cassini
{
/// <summary>
/// response1 的摘要说明
/// </summary>
public class response1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string username = context.Request["username"];
string password = context.Request["password"];
if(string.IsNullOrWhiteSpace(username))
{
context.Response.Write("用户名不能为空" + "\n");
//return;
try
{
context.Response.End(); //本质就是一个抛异常--> 效率低能不用最好不用
//EndHandler(context);
}
catch(Exception ex)
{
context.Response.Write(ex.ToString());
}
}
if (string.IsNullOrWhiteSpace(password))
{
context.Response.Write("密码不能为空"+"\n");
//return;
//context.Response.End();
EndHandler(context);
}
context.Response.Write("插入成功" + "\n");
}
private void EndHandler(HttpContext context)
{
context.Response.Write("终止HttpHandler" + "\n");
context.Response.End(); //子方法中终止进程 只有抛异常才能终止当前执行 不能用return End()本质就是抛出线程异常
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
context.Response.End()的用法和本质:
用法:可以用来终止进程,即当前HttpHandler的执行,
也可以在子方法中终止HttpHandler的执行,
实际在子方法中终止程序,只有一种可能,那就是程序抛异常,所以context.Response.End()得本质就是抛出了线程异常
由于抛异常的效率较低,所以能不能就不用,在主方法中最好还是使用return;终止程序比较好,
但是在子方法中只能使用抛异常终止程序,即使用context.Response.End()终止程序。