开心

一般处理程序(委托字典)

.委托字典代替switch... case.来实现ajax 中不同请求参数处理不同请求。

static Dictionary<string, Func<string>> hs;
static Handler() //一般处理程序的类的构造函数
  {
    hs = new Dictionary<string, Func<string>>();
    hs.Add("add", delegate()
    {
      int id = int.Parse(req("id"));
      string title = req("title");
      return "add";
    });
    hs.Add("update", delegate()
    {
      int id = int.Parse(req("id"));
      string title = req("title");
      return "update";
    });
  }

 public void ProcessRequest (HttpContext context) {

context.Response.ContentType = "text/plain";
  HttpRequest req = context.Request;
  string action = req["action"].ToLower();
  string result = hs[action]();
  context.Response.Write(result);

}

 

static string req(string key)
  {
    return HttpContext.Current.Request[key];
  }

posted @ 2016-01-08 16:49  大喜  阅读(187)  评论(0编辑  收藏  举报
坦然 会增进信任 - 信任 感情才会升华