.ashx 文件用于写web handler的。.ashx文件与.aspx文件类似,可以通过它来调用httphandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程。其实就是带html和c#的混合文件。
.ashx文件适合产生供浏览器处理的、不需要回发处理的数据格式,例如用于生成动态图片、动态文本等内容。很多需要用到此种处理方式。此文档提供一个简单的调用ashx文件的Demo,并贴出关键文件的源码。
以下为Demo中Login.ashx文件中的源码:
基于DEDECMS5.7 SP1制作的漂亮网络工作室整站源码,生成HTML文件。利于收录。整站采用黑色配色,彰显大气。目前仅添加新闻,案例栏目。其他类别请自行在后台添加,并修改首页模板的调用。 安装方法:1.访问:域名/install 按照提示进行安装.2.完成后登陆网站后台---还原数据库3.系统设置---修改网址和网站名称.4.生成整站,后台信息:dede后台用户名:admin后台密码:www
0
public class Login : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "application/json";
//GET方式获取传递的数据
//string username = context.Request.QueryString["username"];
//string password = context.Request.QueryString["password"];
//POST方式获取传递的数据
string username = context.Request.Form["username"];
string password = context.Request.Form["password"];
string message = null;
if (string.IsNullOrEmpty(username))
{
message = "用户名不能为空";
context.Response.Write("{\"success\":false,\"message\":\"" + message + "\"}");//此JSON格式非常重要,否则会执行jquery的的error函数
context.Response.End();
}
if (string.IsNullOrEmpty(password))
{
message = "密码不能为空";
context.Response.Write("{\"success\":false,\"message\":\"" + message + "\"}");
context.Response.End();
}
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
if (username.ToUpper() == "ADMIN" && password == "123")
{
message = "登录成功";
context.Response.Write("{\"success\":true,\"message\":\"" + message + "\"}");
}
else
{
message = "用户名或密码错误";
context.Response.Write("{\"success\":false,\"message\":\"" + message + "\"}");
}
}
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}以下为html中的源码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jsquery访问ashx文件</title>
<script language="javascript" type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script language="javascript" type="text/javascript">
function login() {
$.ajax({
url: 'common/handler/Login.ashx',
type: 'POST',
data: { 'username': $("#txtUsername").val(), 'password': $("#txtPassword").val() },
dataType: 'json',
timeout: 50000,
//contentType: 'application/json;charset=utf-8',
success: function (response) {
alert(response.message);
},
error: function (err) {
alert("执行失败");
}
});
}
</script>
</head>
<body>
<div style="width:400px; height:300px; margin:0 auto; background:#c0c0c0;">
<dl style=" width:270px;">
<dd><span>用户名:</span><input type="text" style=" width:150px;" id="txtUsername" /></dd>
<dd><span>密 码:</span><input type="password" style=" width:150px;" id="txtPassword" /></dd>
<dd><input type="button" style=" width:65px; height:23px; float:right;" onclick="login()" value="登录" /></dd>
</dl>
</div>
</body>
</html>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号