ashx文件使用的基类的实现

来源:互联网 发布:大连网络推广 编辑:程序博客网 时间:2024/05/18 00:55

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

一、首先创建一个通用的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 0
原创粉丝点击