ashx文件使用的基类的实现

在会员管理系统或者后台管理系统中。可以创建基类,以实现基本的验证,及一些公用的方法或函数。而对于网站中一些一般处理程序,也有类似的需求。实现方法

一、首先创建一个通用的ashx文件,其他页面要继承此文件,这里不能单独使用cs类来实现

BaseHandle.ashx

代码的实现:

/// <summary>    
/// ashx页面统一调用的基类 的摘要说明    
/// </summary>    
public class BaseHandle : IHttpHandlerIRequiresSessionState    
{
 
    protected NiuMaiFacade facade = new NiuMaiFacade();
    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";
      //判断登录是否超时            
	    if (!facade.CheckLogin())
       {
           JSHelper.Write("登录超时,请重新登录");
           context.Response.End();
       }
       AjaxProcess(context);
 
     }
 
 
 
        public bool IsReusable
        {
            get            
			{
                return false;
            }
        }
        
	/// <summary>        
	/// 虚拟方法        
	/// </summary>        
	/// <param name="context"></param>        
	public virtual void AjaxProcess(HttpContext context) { }
}


AjaxProcess这个方法必须,要在其他调用这个基类的页面中使用

二、调用方法

/// <summary>    
/// ProductChangeState 的摘要说明    
/// </summary>    
public class ProductChangeState : BaseHandle    
{
 
        public override void AjaxProcess(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            
            //上架||下架            
			if (RequestHelper.GetFormString("act") == "chgState")
            {
                //EventLog.WriteLog("b");
                 int iid = RequestHelper.GetFormString("Id").ToInteger();
                int iState = RequestHelper.GetFormString("attr").ToInteger();
                int iverify = 1;
 
                string strSql = string.Empty;
                if (RequestHelper.GetFormString("attr") == "1")
                {
                    strSql = string.Format("update producta set iState={0},iverify={1} where iid={2}", iState, iverify, iid);
                }
                else                {
                    strSql = string.Format("update producta set iState={0} where iid={1}", iState, iid);
                }
                if (facade.ExecuteSql(strSql) > 0)
                {
                    JSHelper.Write("ok");
                    context.Response.End();
                }
                else                {
                    JSHelper.Write("操作失败,请与客服人员联系");
                    context.Response.End();
                }
            }
            //作废操作            
			if (RequestHelper.GetFormString("act") == "chgDel")
            {
                if (!RequestHelper.GetFormString("Id").IsInteger())
                {
                    JSHelper.Write("参数错误");
                    context.Response.End();
                }
                int iid = RequestHelper.GetFormString("Id").ToInteger();
                string sql = string.Format("select istate from producta where iid={0}", iid);
                int istate = facade.GetScalarBySql(sql).ToInteger();
                if (istate == 2)  //如果是作废状态,则启用                {
                    sql = string.Format("update producta set iState=0 where iid={0}", iid);
                }
                else  //如果不是作废状态,则作废                {
                    sql = string.Format("update producta set iState=2 where iid={0}", iid);
                }
                if (facade.ExecuteSql(sql) > 0)
                {
                    JSHelper.Write("ok");
                    context.Response.End();
                }
                else                {
                    JSHelper.Write("操作失败,请与客服人员联系");
                    context.Response.End();
                }
            }
            context.Response.Write("参数错误");
        }
 
 
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值