用反射封装HttpHandler,实现通过action方法名调用方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.SessionState;

namespace DIDAO.Admin.Common
{
    public class BaseController : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            //通过反射 封装HttpHandler
            //通过寻找方法名 来确定执行方法
            //需要约定方法:public void list(HttpContext context) //方法名和action一致
            #region 检查用户是否登录
            LoginHelper.CheckHasLogin(context);
            #endregion
            string action = context.Request["action"];
            if(action==null)
            {
                throw new Exception("未找到action:"+action);
            }
            Type type = this.GetType(); //this是被new的对象,即子类CategoryController等
            try
            {
                MethodInfo method = type.GetMethod(action);
                method.Invoke(this, new object[] { context }); //调用this对象的method方法 (参数列表是object[],其中一个参数是context)
            }
            catch(Exception ex) //如果找不到这个action方法,就说明这个action是错误的/不存在的
            {
                throw new Exception("未知的action:"+action);
            }
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using DIDAO.Admin.Common;
using DIDAO.BLL;
using DIDAO.Common;
using DIDAO.Model;

namespace DIDAO.Admin.Course
{
    /// <summary>
    /// CategoryController 的摘要说明
    /// </summary>
    public class CategoryController : BaseController
    {
        MyORM_BLL myORM_BLL = new MyORM_BLL();
        //public void ProcessRequest(HttpContext context)
        //{
            //context.Response.ContentType = "text/html";
            //#region 检查用户是否登录
            //LoginHelper.CheckHasLogin(context);
            //#endregion
            //string action = context.Request["action"];
            //if(action=="list") //类别列表
            public void list(HttpContext context)
            {
                #region 类别列表
                string pidStr = context.Request["pid"];
                int pid = VolidHelper.CheckStrToInt(pidStr);
                //要展示类别列表,需要该类别副PID下面没有子类别,确保不会同时存在(子类别和课程)的混乱情况
                bool flag = new CourseBLL().CheckHasCourseInPid(pid);
                if(flag) 
                {
                    RazorHelper.RazorParse(context, "~/error.cshtml", new { Msg = "该类别ID下面含有课程,不能进行子类别的显示和添加" });
                    return;
                }
                //
                List<object> list = new CourseBLL().SelectCategoryByPID(pid);
                RazorHelper.RazorParse(context, "~/Course/CategoryList.cshtml", new { cates = list, pid = pid });
                #endregion
            }
            //else if(action=="addnew") //新增展示
            public void addnew(HttpContext context)
            {
                #region 新增展示
                AdminHelper.CheckHasPower(context, "新增视频类别");
                string pidStr = context.Request["pid"];
                int pid = VolidHelper.CheckStrToInt(pidStr);
                RazorHelper.RazorParse(context, "~/Course/CategoryAlter.cshtml", new { action = "addnew", id = "", no = "", name = "", pid = pid }); 
                #endregion
            }
            //else if (action == "edit") //编辑展示
            public void edit(HttpContext context)
            {
                #region 编辑展示
                AdminHelper.CheckHasPower(context, "编辑视频类别");
                string idStr = context.Request["id"];
                int id = VolidHelper.CheckStrToInt(idStr);
                object obj = myORM_BLL.SelectModelById(typeof(TD_VIDEOCATEGORY), 1, id);
                if (obj == null)
                {
                    RazorHelper.RazorParse(context, "~/error.cshtml", new { Msg = "未查询到该类别:" + id });
                    return;
                }
                TD_VIDEOCATEGORY cate = obj as TD_VIDEOCATEGORY;
                RazorHelper.RazorParse(context, "~/Course/CategoryAlter.cshtml", new { action = "edit", id = id, no = cate.NO, name = cate.NAME, pid = cate.PID }); 
                #endregion
            }
            //else if (action == "disable") //类别禁用
            public void disable(HttpContext context)
            {
                #region 类别禁用
                AdminHelper.CheckHasPower(context, "禁用视频类别");
                string idStr = context.Request["id"];
                int id = VolidHelper.CheckStrToInt(idStr);
                string pidStr = context.Request["pid"];
                int pid = VolidHelper.CheckStrToInt(pidStr);
                //获得类别实例
                object obj = myORM_BLL.SelectModelById(typeof(TD_VIDEOCATEGORY), id);
                if(obj==null)
                {
                    RazorHelper.RazorParse(context, "~/error.cshtml", new { Msg = "未查询到该类别:" + id });
                    return;
                }
                TD_VIDEOCATEGORY cate = obj as TD_VIDEOCATEGORY;
                long currid = (long)LoginHelper.GetSessionID(context);
                bool falg = myORM_BLL.DeleteOrDisableModelByUpdateStatus(typeof(TD_VIDEOCATEGORY),3,currid,DateTime.Now,  id);
                AdminHelper.RecordOperateLog(context, "禁用视频类别:" + cate.NAME);
                context.Response.Redirect("/Course/CategoryController.ashx?action=list&pid=" + pid); 
                #endregion
            }
            //else if(action=="delete") //类别真删除
            public void delete(HttpContext context)
            {
                #region 类别真删除
                AdminHelper.CheckHasPower(context, "真删除视频类别");
                string idStr = context.Request["id"];
                int id = VolidHelper.CheckStrToInt(idStr);
                string pidStr = context.Request["pid"];
                int pid = VolidHelper.CheckStrToInt(pidStr);
                //获得类别实例
                object obj = myORM_BLL.SelectModelById(typeof(TD_VIDEOCATEGORY), id);
                if (obj == null)
                {
                    RazorHelper.RazorParse(context, "~/error.cshtml", new { Msg = "未查询到该类别:" + id });
                    return;
                }
                TD_VIDEOCATEGORY cate = obj as TD_VIDEOCATEGORY;
                //根绝类别Id 真删类别及下面的所有课程
                new CourseBLL().DeleteTrueCategoryById(id);
                AdminHelper.RecordOperateLog(context, "真删除视频类别:"+cate.NAME);
                context.Response.Redirect("/Course/CategoryController.ashx?action=list&pid=" + pid);  
                #endregion
            }
            //else if (action == "save") //类别保存
            public void save(HttpContext context)
            {
                #region 类别保存
                string saveAction = context.Request["saveAction"];
                string noStr = context.Request["no"];
                string name = context.Request["name"];
                string pidStr = context.Request["pid"];
                int pid = VolidHelper.CheckStrToInt(pidStr);
                #region 验证 非空及格式
                if (!VolidHelper.CheckStringIsAllIntNotComma(noStr))
                {
                    AjaxHelper.WriteJson(context, "error", "序号必须是数字");
                    return;
                }
                if (string.IsNullOrWhiteSpace(name))
                {
                    AjaxHelper.WriteJson(context, "error", "请填写类别名称");
                    return;
                }
                #endregion
                int no = VolidHelper.CheckStrToInt(noStr);
                TD_VIDEOCATEGORY cateNew = new TD_VIDEOCATEGORY();
                cateNew.DELETEBY = null;
                cateNew.DELETEDATE = null;
                cateNew.NAME = name;
                cateNew.NO = no;
                cateNew.STATUS = 1;
                cateNew.PID = pid;
                long currid = (long)LoginHelper.GetSessionID(context);

                if (saveAction == "addnew") //新增保存
                {
                    #region 新增保存
                    AdminHelper.CheckHasPower(context, "新增视频类别");
                    bool flag = new CourseBLL().AddCategory(cateNew,currid);
                    if (!flag)
                    {
                        AjaxHelper.WriteJson(context, "error", "新增视频类别失败:" + name);
                        return;
                    }
                    AdminHelper.RecordOperateLog(context, "新增视频类别:"+name);
                    AjaxHelper.WriteJson(context, "ok", "新增视频类别 成功");
                    #endregion
                }
                else if (saveAction == "edit") //编辑保存
                {
                    #region 编辑保存
                    AdminHelper.CheckHasPower(context, "编辑视频类别");
                    string idStr = context.Request["id"];
                    int id = VolidHelper.CheckStrToInt(idStr);
                    bool flag = new CourseBLL().EditCategory(cateNew, id,currid);
                    if (!flag)
                    {
                        AjaxHelper.WriteJson(context, "error", "编辑视频类别失败:" + name);
                        return;
                    }
                    AdminHelper.RecordOperateLog(context, "编辑视频类别:" + name);
                    AjaxHelper.WriteJson(context, "ok", "编辑视频类别 成功");
                    #endregion
                }
                else
                {
                    throw new Exception("未知的saveAction:" + saveAction);
                } 
                #endregion
            }
            //else
            //{
            //    throw new Exception("未知的action:"+action);
            //}
        //}

        //public bool IsReusable
        //{
        //    get
        //    {
        //        return false;
        //    }
        //}
    }
}
CategoryController.cs
原文地址:https://www.cnblogs.com/adolphyang/p/4842000.html