ashx的学习

                嘿嘿,今天我们休息,本来是想总结一下前两周学习的javascript和jquery,但是感觉好困哦,就没有认真地学习啦,于是做了一个小小的练习,刚开始学习html使用在项目中还是蛮高兴的啦,下面就简单的总结一下这个小小的登录页面。

         一.html的静态页面

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style>
        form{
             margin-right:500px;
             margin-top:10px;
             300px;
             height:300px;   
          }
    </style>
</head>
<body>
    <form action="first.ashx" method="post">
        <table>
            <tr><td>用户名:</td><td><input type="text" name="txtname" /></td></tr><br />
            <tr><td>密 &nbsp;码:</td><td><input type="text" name="txtpwd"  /></td></tr><br />
            <tr><td><input type="submit" name="submit" value="登录" /></td>
                <td><input type="button" name="btnFindPwd" value="找回密码 " /></td></tr>
        </table>
    </form>
</body> 

                    这里是写了一个简单的html页面,实现其登录界面的样式。

                     二.ashx的文件代码

using System;
using System.Web;
using System.IO;
using UseiInfoModel;
using UserInfoBll;

public class first : IHttpHandler {  
       public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";          //接受的是html格式的文档

        string path = context.Request.MapPath("FirstHtml.html");   //获取文档的路径
        string html = File.ReadAllText(path);               //读取文档
        context.Response.Write(html);         //然后写入,即返回给我们的是html页面
            
        string name=context.Request.Form["txtname"];     //获取txtname
        string pwd = context.Request.Form["txtpwd"];    //获取txtpwd
        if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(pwd))    //当文本框不为空
        {
            Userinfobll bll = new Userinfobll();
            Userinfomodelcs model = bll.GetLoginByNamePwd(name, pwd);   //调用数据
            if(bll!=null&&string.IsNullOrEmpty(model.Username)&&string.IsNullOrEmpty(model.Pwd))
            {
                context.Response.Clear();
                context.Response.Write("欢迎" + model.Username + "登陆成功");    //相应报文
            }
        }
    }   
    public bool IsReusable {
        get {
            return false;
        }
    }
}

                这就是新学习ashx文件,实现请求报文和响应报文。在这里实现了html与服务器的交互。
                 三.bll层和dal层的代码

public class Userinfobll
    {
        Userinfodal dal = new Userinfodal();
        public Userinfomodelcs GetLoginByNamePwd(string name, string pwd)
        {
            return dal.GetLoginByNamePwd(name,pwd);
        }
    }
 public class Userinfodal
    {
        public Userinfomodelcs GetLoginByNamePwd(string name,string pwd)
        {//Id, Username, Pwd
            string sql = "select Id,Username,Pwd from UserLogin where Username=@name and Pwd=@pwd";
            SqlParameter[] parms ={
                                    new SqlParameter("@name",name),
                                    new SqlParameter("@pwd",pwd)
                                };
            SqlDataReader reader= DBHelp.ExecuteReader(sql,parms);
            Userinfomodelcs model = new Userinfomodelcs();
            if (reader.Read())
            {
                model.Id = Convert.ToInt32(reader[0]);
                model.Username = reader[1].ToString();
                model.Pwd = reader[1].ToString();
            }
            return model;
        }
    }
public static class DBHelp
    {
        private static string connection = ConfigurationManager.ConnectionStrings["sql"].ToString();
        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] parms)
        {
            SqlConnection conn = new SqlConnection(connection);
            conn.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = sql;
                cmd.Parameters.AddRange(parms);
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandTimeout = 5;
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
        }
    }
 public class Userinfomodelcs
    {//Id, Username, Pwd
        public int Id { set; get; }
        public string Username { set; get; }
        public string Pwd { set; get; }
    }

               嘿嘿,一直以为使用aspx实现其数据的提交与响应,今天学习了ashx感觉这个很奇怪,使用起来还是蛮不熟悉的,首先在实现其代码的过程中感觉不是直接和页面交互,而是一切和数据有关的和页面和有关的都要去实现,并不是很简单的那样,嘿嘿,这只是个人的意见,不知道大家在学习这个时间是不是这样的感觉那,怎么说那?可能接下来我们要学习ajax,学习完这个就好多啦,与页面的交互会更加的方便吧,但是之前也没怎么接触ajax,只是看到啦和js中使用,具体的还是不了解的,就写到这里啦,最近学习的理论知识还没有总结,感觉真的是需要再给点时间理解一下,需要了解清楚在总结。要继续努力!

我是小白,新建立了一个的群:461431726,希望在这里和大家一起交流,共同学习。前端的话建议加群:646564351,谢谢
原文地址:https://www.cnblogs.com/dyxd/p/4231315.html