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

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()终止程序。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值