一般处理程序(委托字典)
.委托字典代替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];
}