asp.net ashx处理程序中switch case的替代方案总结

  • 2019 年 10 月 3 日
  • 筆記

[toc]


??? asp.net ???????????????ashx?????????????????handler?????????ajax??????????action?????handler?????action??????????????????????????switch…case????????????switch??????????case????????????????????????????????case??????????????????????? ??????switch…case?????????????

1????????switch…case;

???handler???????????????key?action????value?Func??????action??????????????? ???????

namespace WebApplication1  {        public class Handler1 : IHttpHandler      {          static Dictionary<string, Action<HttpContext>> MapActions = new Dictionary<string, Action<HttpContext>>(StringComparer.OrdinalIgnoreCase)          {              {"Add", Add},              {"Sub", Sub}          };            public void ProcessRequest(HttpContext context)          {              context.Response.ContentType = "text/plain";              try              {                  var action = context.Request["Action"];                  if (string.IsNullOrEmpty(action))                  {                      context.Response.StatusCode = (int) HttpStatusCode.BadRequest;                  }                    if (MapActions.ContainsKey(action))                  {                      var actionFun = MapActions[action];                      if (actionFun != null)                      {                          actionFun(context);                          //?                          //actionFun.Invoke(context);                      }                      else                      {                          context.Response.StatusCode = (int) HttpStatusCode.NotImplemented;                      }                  }                  else                  {                      context.Response.StatusCode = (int) HttpStatusCode.NotFound;                  }              }              catch (Exception e)              {                  context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;              }              finally              {                  context.Response.End();              }          }            public static void Add(HttpContext context)          {              int num1 = int.Parse(context.Request["Num1"]);              int num2 = int.Parse(context.Request["Num2"]);              int result = num1 + num2;              context.Response.Write(result);          }            public static void Sub(HttpContext context)          {              int num1 = int.Parse(context.Request["Num1"]);              int num2 = int.Parse(context.Request["Num2"]);              int result = num1 - num2;              context.Response.Write(result);          }            public bool IsReusable          {              get              {                  return false;              }          }      }  }  

2???????switch…case?

??????action???????????? ???????

namespace WebApplication1  {        public class Handler2 : IHttpHandler      {          static readonly Type[] SearchParamType = new[] { typeof(HttpContext) };          public void ProcessRequest(HttpContext context)          {              var result = ActionInvoke(context);                context.Response.ContentType = "text/plain";              context.Response.Write(result);          }          private object ActionInvoke(HttpContext ctx)          {              var actionFun = this.GetType().GetMethod("ProcessAction_" + ctx.Request["action"] ?? "",                  BindingFlags.NonPublic |                  BindingFlags.IgnoreCase |                  BindingFlags.Instance |                  BindingFlags.Public,                  null,                  SearchParamType,                  null);              if (null == actionFun)              {                  return "UnknowAction";              }              return actionFun.Invoke(this, new[] { ctx });          }          public int ProcessAction_Add(HttpContext context)          {              int num1 = int.Parse(context.Request["Num1"]);              int num2 = int.Parse(context.Request["Num2"]);              int result = num1 + num2;              return result;          }            public int ProcessAction_Sub(HttpContext context)          {              int num1 = int.Parse(context.Request["Num1"]);              int num2 = int.Parse(context.Request["Num2"]);              int result = num1 - num2;              return result;          }            public bool IsReusable          {              get              {                  return false;              }          }      }  }  

3???????

????????????????????????????

4?????

????????????????????????????????????????????????? ????

5???

????????case??????????????switch??????????????? ?????????????????case????????????????????????????

6???

???switch???????????????https://blog.csdn.net/qq_21381465/article/details/51298808 ????? https://www.cnblogs.com/insus/p/4134383.html