ashx
一般处理程序(HttpHandler)是·NET众多web组件的一种,ashx是其扩展名。一个httpHandler接受并处理一个http请求,类比于Java中的servlet。类比于在Java中需要继承HttpServlet类。在net中需要实现IHttpHandler接口,这个接口有一个IsReusable成员,一个待实现的方法ProcessRequest(HttpContextctx) 。程序在processRequest方法中处理接受到的Http请求。成员IsReusable指定此IhttpHandler的实例是否可以被用来处理多个请求。
.ashx程序适合产生供浏览器处理的、不需要回发处理的数据格式,例如用于生成动态图片、动态文本等内容。
例子:1 前台Html:
ABC
2 JQuery实现前台html中的Login():
function Login() { $('#fm').form('submit', { url: "../HttpHandler/LoginHandler.ashx?action=Login", onSubmit: function () { if (validate()) { return $(this).form('validate'); } else { return false; } //return $(this).form('validate'); }, error: function () { $.messager.alert('错误', '操作失败!', 'error'); }, success: function (result) { var result = eval('(' + result + ')'); if (result.success) { location.href = "../Pages/MainPage.htm"; } else { $.messager.alert('提示', result.msg, 'warning'); } } });}
3 后台一般程序处理:LoginHandler.ashx:
namespace Web.HttpHandler{ public class LoginHandler : IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string sReturnJson = string.Empty; string action = ParamsofEasyUI.RequstString("action"); switch (action) { case "Login": sReturnJson = Login(); break; case "CheckLogin": sReturnJson = CheckLogin(); break; case "GetUserInfo": sReturnJson = GetUserInfo(); break; default: break; } context.Response.Write(sReturnJson); context.Response.End(); } private string GetUserInfo() { string result = string.Empty; //读取保存的Cookie信息 HttpCookie cookies = HttpContext.Current.Request.Cookies["USER_COOKIE"]; if (cookies != null) { result += "{UserName:'"; result += cookies["UserName"]; result += "',UserPassword:'"; result += cookies["UserPassword"]; result += "',Checked:true}"; } else { result += "{UserName:'"; result += "',UserPassword:'"; result += "',Checked:false}"; } return result; } private string CheckLogin() { if (HttpContext.Current.Session.Keys.Count == 0) { return "{success:false}"; } string curUser = HttpContext.Current.Session["userName"].ToString(); if (curUser == null) { return "{success:false}"; } if (HttpContext.Current.Application.AllKeys.Contains(curUser)) { return "{success:true}"; } else { return "{success:false}"; } } private string Login() { string userName = ParamsofEasyUI.RequstForm("userName"); string password = ParamsofEasyUI.RequstForm("password"); bool blChecked = ParamsofEasyUI.RequstBool("remember"); string localPsw = Encoding.Default.GetString(Convert.FromBase64String(ConfigTools.Get(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), ConfigurationManager.AppSettings["ConfigFile"]), "CODE"))); if (userName.ToLower() == "admin" && password == localPsw) { if (blChecked) { HttpCookie cookie = new HttpCookie("USER_COOKIE"); //所有的验证信息检测之后,如果用户选择的记住密码,则将用户名和密码写入Cookie里面保存起来。 cookie.Values.Add("UserName", userName); cookie.Values.Add("UserPassword", password); //这里是设置Cookie的过期时间,这里设置一个星期的时间,过了一个星期之后状态保持自动清空。 cookie.Expires = System.DateTime.Now.AddDays(7.0); HttpContext.Current.Response.Cookies.Add(cookie); } else { HttpCookie cookie = HttpContext.Current.Request.Cookies["USER_COOKIE"]; if (cookie != null) { //如果用户没有选择记住密码,那么立即将Cookie里面的信息清空,并且设置状态保持立即过期。 HttpContext.Current.Response.Cookies["USER_COOKIE"].Expires = DateTime.Now; } } string newGuid = Guid.NewGuid().ToString(); HttpContext.Current.Application.Lock(); HttpContext.Current.Application["admin"] = newGuid; HttpContext.Current.Application.UnLock(); HttpContext.Current.Session[HttpContext.Current.Session.SessionID] = newGuid; HttpContext.Current.Session.Add("UserName", userName); return "{success:true}"; } else { return "{success:false,msg:'用户名或密码错误!'}"; } } public bool IsReusable { get { return false; } } }}
namespace Web.HttpHandler{ public class ParamsofEasyUI { public static string RequstForm(string name) { return (HttpContext.Current.Request.Form[name] == null ? string.Empty : HttpContext.Current.Request.Form[name].ToString().Trim()); } public static string RequstString(string sParam) { return (HttpContext.Current.Request[sParam] == null ? string.Empty : HttpContext.Current.Request[sParam].ToString().Trim()); } }}