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());
}
}
}

一般处理程序HttpHandler的应用的更多相关文章

  1. 002-一般处理程序(HttpHandler)

    一般处理程序(HttpHandler):是一个实现System.Web.IHttpHandler接口的特殊类.任何一个实现了IHttpHandler接口的类,是作为一个外部请求的目标程序的前提.(凡是 ...

  2. 使用一般处理程序HTTPHandler下载文件

    一般来说我们可以用HTTPHandler来处理一些简单的逻辑,比如验证码.下载文件等. 以下载word文档为例讲解一下如何在HHTPHandler中下载文件,不限于word文档,如果下载其他文件,需要 ...

  3. EasyUI - 使用一般处理程序 HttpHandler (.ashx)

    以easyui中的panel中,使用url加载数据为列. 效果: html代码: <div id="p" style="padding: 10px;"&g ...

  4. 【IHttpHandler】HttpModule,HttpHandler,HttpHandlerFactory简单使用

    这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时候又不太确定.本文通过一个简单的例子来直观的比较一下这三个对象的使用. HttpModule:Http模块,可以在页面处理前后.应 ...

  5. HttpModule,HttpHandler,HttpHandlerFactory

    HttpModule:Http模块,可以在页面处理前后.应用程序初始化.出错等时候加入自己的事件处理程序. HttpHandler:Http处理程序,处理页面请求 HttpHandlerFactory ...

  6. .net-一般处理程序及生命周期

    IsReusable属性用来表示在IHttpHandlerFactory对象创建IHttpHandler的时候是否能够将这个Handler存入池中以便重用. 一般处理程序(HttpHandler):是 ...

  7. 【.net项目中。。】.net一般处理程序使用方法

    1.基本原理图 IsReusable属性用来表示在IHttpHandlerFactory对象创建IHttpHandler的时候是否能够将这个Handler存入池中以便重用. 一般处理程序(HttpHa ...

  8. C#强化系列:HttpModule,HttpHandler,HttpHandlerFactory简单使用

    这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时候又不太确定.本文通过一个简单的例子来直观的比较一下这三个对象的使用.HttpModule:Http模块,可以在页面处理前后.应用 ...

  9. asp.net页面生命周期

    Asp.Net页面生命周期 本文转载自:http://www.cnblogs.com/xhwy/archive/2012/05/20/2510178.html 一.什么是Asp.Net页面生命周期 当 ...

随机推荐

  1. CentOS下SSH远程免密登录服务器

    .5服务器上配置,通过ssh远程免密登录192. 1.安装SSH,此处省略 2.生成公钥和私钥,生成的秘钥默认在/root/.ssh/文件夹里面 [root@localhost ~ ::&&a ...

  2. Vue.js基础拾遗

    本篇目录: 模版语法 插值 指令 v-bind指令 v-on指令 计算属性与侦听器 计算属性VS方法 计算属性VS侦听属性 Class与Style绑定 绑定HTML Class 绑定内联样式 条件渲染 ...

  3. centos7之使用最新版的kubeadm体验k8s1.12.0

    1.环境准备 centos7 .docker-ce18.06.1-ce.kubeadm.kubelet.kubectl 2.安装 yum安装,准备repo文件 docker: [docker-ce-s ...

  4. VGG 论文研读

    VERY DEEP CONVOLUTIONAL NETWORKS FOR LARGE-SCALE IMAGE RECOGNITION 论文地址 摘要 研究主要贡献是通过非常小的3x3卷积核的神经网络架 ...

  5. 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)

    目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...

  6. 关于Markdown插入图片路径错误的问题

    关于Markdown插入图片路径错误的问题 开发问题 解决方法  妈耶,连续一天写2篇博客,也是醉了,这篇博客主要是介绍关于Markdown插入图片路径错误的问题 在上篇中,我介绍了一下Markdow ...

  7. 分布式理论(二)——Base 理论

    前言 在前文 分布式理论(一) -- CAP 定理 中,我们说,CAP 不可能同时满足,而分区容错是对于分布式系统而言,是必须的.最后,我们说,如果系统能够同时实现 CAP 是再好不过的了,所以出现了 ...

  8. 并发编程 —— 自己写一个异步回调 API

    1. 前言 在并发编程中,异步回调的效率不言而喻,在业务开发中,如果由阻塞的任务需要执行,必然要使用异步线程.并且,如果我们想在异步执行之后,根据他的结果执行一些动作. JDK 8 之前的 Futur ...

  9. MVC使用jQuery.ajax()删除数据

    jQuery.ajax()可以简写为$.ajax().以前有写过MVC删除的实现,如<MVC实现删除数据库记录> http://www.cnblogs.com/insus/p/336804 ...

  10. c#中Socket网络通信的入门

    请访问 http://balabiu.com/?p=16 后续本文更新将在这里: 将设计服务器端异步接受客户端连接和客户端消息.