ashx
一般处理程序(HttpHandler)是·NET众多web组件的一种,ashx是其扩展名。一个httpHandler接受并处理一个http请求,类比于Java中的servlet。类比于在Java中需要继承HttpServlet类。在net中需要实现IHttpHandler接口,这个接口有一个IsReusable成员,一个待实现的方法ProcessRequest(HttpContextctx) 。程序在processRequest方法中处理接受到的Http请求。成员IsReusable指定此IhttpHandler的实例是否可以被用来处理多个请求。
.ashx程序适合产生供浏览器处理的、不需要回发处理的数据格式,例如用于生成动态图片、动态文本等内容。
例子:
1 前台Html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ABC</title>
<link rel="stylesheet" type="text/css" href="../easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="../easyui/themes/icon.css" />
<script type="text/javascript" src="../easyui/jquery.min.js"></script>
<script type="text/javascript" src="../easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="index.js"></script> <style type="text/css">
.code
{
background-image:url(/Images/verifyCode.png);
font-family:Arial;
font-style:italic;
color:Red;
border:0;
padding:2px 3px;
letter-spacing:3px;
font-weight:bolder;
text-align:center;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
-khtml-user-select: none;
user-select: none;
}
</style>
</head>
<body>
<div style="margin:0 auto;position:absolute;top:50%;margin-top:-115px;left:50%;margin-left:-200px">
<form id="fm" method="post" novalidate>
<div id="login" class="easyui-panel" title="登录" style="width:400px;padding:30px 70px 20px 70px;">
<div style="margin-bottom:10px">
<input id="userName1" name="userName" class="easyui-textbox" style="width:100%;height:40px;padding:12px" data-options="prompt:'用户名',iconCls:'icon-man',iconWidth:38">
</div>
<div style="margin-bottom:20px">
<input id="password1" name="password" class="easyui-textbox" type="password" style="width:100%;height:40px;padding:12px" data-options="prompt:'Password',iconCls:'icon-lock',iconWidth:38">
</div>
<div style="margin-bottom:20px; display:inline">
<input id="verifyCode" name="verifyCode" type="text" style="width:150px; height:12px; padding:12px; border: 1px solid #95BBE7 " />
<input id="checkCode" name="checkCode" value="XH59" class="code" type="text" onclick="createCode()" style="width:60px" readonly />
</div>
<div style="margin-top:20px; margin-bottom:20px">
<input id="remember1" name="remember" type="checkbox">
<span>记住密码</span>
</div>
<div>
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="Login()" style="padding:5px 0px;width:100%;">
<span style="font-size:14px;">登录</span>
</a>
</div>
</div>
</form>
</div>
</body>
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 == )
{
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());
}
}
}