.net 开发中对ASHX文件用处的疑问
但是我想知道我这样写代码的弊端在哪里。或者说有致命的问题,那么我肯定就要把它改正了。
然而我们这个项目 有很多模块都需要登录,有点整合的意味。所以我一定要把表单处理给通用化。
不能写在页面的cs类里面、
//我前台是依靠 UserAction.ashx?Method=UserNameLogin 这样的action值把参数提交过来的
然后调用业务逻辑层做操作,最终返回结果跳转页面。
<%@ WebHandler Language="C#" Class="UserAction" %>
using System;
using System.Web;
using System.Web.SessionState;
public class UserAction : IHttpHandler,IRequiresSessionState{
private HttpContext Mcontext;
private string FromUrl;
public void ProcessRequest(HttpContext context) {
context.Response.ContentType = "text/html;charset=utf-8"; ;
Mcontext = context;
string Method = context.Request["Method"];
FromUrl = context.Request.ServerVariables["HTTP_REFERER"];//保存从何页面发送来的请求,权限不通过则返回
switch (Method)
{
case "UserNameLogin" : UserNameLogin(); break;
case "UserPhoneLogin": UserPhoneLogin(); break;
case "UserExit": UserExit(); break;
}
}
public void UserExit() //退出用户
{
Mcontext.Session["User"] = null;
Mcontext.Session["BUser"] = null;
Mcontext.Response.Write("<script> alert('退出成功!');</script>");
Mcontext.Response.Write("<script> location.href='index.aspx'; </script>");
}
public void UserPhoneLogin()//根据手机号登录
{
string UserType = "";
string UserName = "";
string UserPassword = "";
UserType = Mcontext.Request["usertype"];
UserName = Mcontext.Request["username"];
UserPassword = Mcontext.Request["password"];
if (UserType == "1")//个人登录
{
FYM_User fu = new FYM_User();
fu.U_phone = UserName;
fu.U_password = UserPassword;
UserBLL ub = new UserBLL();
fu = ub.LoginByUpPwd(fu);
if (fu.id != 0)
{
Mcontext.Session["User"] = fu;
Mcontext.Session["BUser"] = null;
Mcontext.Response.Write("<script> alert('登录成功!欢迎您:" + fu.U_username + "您的用户类别为个人用户');</script>");
Mcontext.Response.Write("<script> location.href='index.aspx'; </script>");
}
else
{
Mcontext.Response.Write("<script> alert('登录失败,个人手机号或密码错误!');</script>");
Mcontext.Response.Write("<script> location.href='" + FromUrl + "'; </script>");
}
}
if (UserType == "2")//企业登录
{
FYM_BusinessUser fu = new FYM_BusinessUser();
fu.B_userphone = UserName;
fu.B_password = UserPassword;
BUserBLL ub = new BUserBLL();
fu = ub.LoginByUpPwd(fu);
if (fu.id != 0)
{
Mcontext.Session["User"] = null;
Mcontext.Session["BUser"] = fu;
Mcontext.Response.Write("<script> alert('登录成功!欢迎您:" + fu.B_username + "您的用户类别为企业用户');</script>");
Mcontext.Response.Write("<script> location.href='index.aspx'; </script>");
}
else
{
Mcontext.Response.Write("<script> alert('登录失败,企业联系人电话或密码错误!');</script>");
Mcontext.Response.Write("<script> location.href='" + FromUrl + "'; </script>");
}
}
}
public void UserNameLogin() //根据用户名登录
{
string UserType = "";
string UserName = "";
string UserPassword = "";
UserType = Mcontext.Request["usertype"];
UserName = Mcontext.Request["username"];
UserPassword = Mcontext.Request["password"];
if (UserType == "1")//个人登录
{
FYM_User fu = new FYM_User();
fu.U_username = UserName;
fu.U_password = UserPassword;
UserBLL ub = new UserBLL();
fu = ub.LoginByUnPwd(fu);
if (fu.id != 0)
{
Mcontext.Session["User"] = fu;
Mcontext.Session["BUser"] = null;
Mcontext.Response.Write("<script> alert('登录成功!欢迎您:"+fu.U_username+"您的用户类别为个人用户');</script>");
Mcontext.Response.Write("<script> location.href='index.aspx'; </script>");
}
else
{
Mcontext.Response.Write("<script> alert('登录失败,个人用户名或密码错误!');</script>");
Mcontext.Response.Write("<script> location.href='" + FromUrl + "'; </script>");
}
}
if (UserType == "2")//企业登录
{
FYM_BusinessUser fu = new FYM_BusinessUser();
fu.B_username = UserName;
fu.B_password = UserPassword;
BUserBLL ub = new BUserBLL();
fu = ub.LoginByUnPwd(fu);
if (fu.id != 0)
{
Mcontext.Session["User"] = null;
Mcontext.Session["BUser"] = fu;
Mcontext.Response.Write("<script> alert('登录成功!欢迎您:" + fu.B_username + "您的用户类别为企业用户');</script>");
Mcontext.Response.Write("<script> location.href='index.aspx'; </script>");
}
else
{
Mcontext.Response.Write("<script> alert('登录失败,企业用户名或密码错误!');</script>");
Mcontext.Response.Write("<script> location.href='" + FromUrl + "'; </script>");
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
相关推荐
ashx文件的使用
简单介绍 ashx 的使用,学习很有帮助
【ASP.NET编程知识】ashx介绍以及ashx文件与aspx文件之间的区别.docx
【ASP.NET编程知识】ashx介绍以及ashx文件与aspx文件之间的区别.docx
asp.net利用ashx文件实现文件的上传功能
原来以为文件上传是一个比较简单的功能,结果搞了一个晚上才搞定~这里主要介绍两种方法实现。 方法一:Form表单提交 html代码: <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>上传文件</title> [removed][removed] </head> <body> <form action=UploadHandler.ashx method=post enctype=multipart/form-data>
asp.net中AJAX与ashx页面交互简单示例
在asp.NET中通过不用任何控件向ashx页面发送ajax请求,返回数据到前台,在vs2008中可直接运行并有详细的注释。
ashx介绍以及ashx文件与aspx文件之间的区别
ashx是什么文件? .ashx 文件用于写web handler的。.ashx文件与.aspx文件类似,可以通过它来调用HttpHandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程。其实就是带HTML和C#的混合文件。 .ashx文件适合产生供浏览器处理的、不需要回发处理的数据格式,例如用于生成动态图片、动态文本等内容。 ashx文件是.net 2.0新加的文件类型(其实在.net 1.0下已经可用,但是没有公开提供). ashx文件和aspx文件有什么不同? 我们先新建一个ashx文件看看: 代码示例:当然你完全可以用.aspx 的文件后缀。使用.ashx 可以让你


