菜鸟学asp.net遇到的问题和解决方案

来源:互联网 发布:大学英语教材听力软件 编辑:程序博客网 时间:2024/06/05 18:53

          在asp.net刚开始学习的时候,会遇到如建立空网站还是网站,这是有区别的,空网站在VS2008中就是空的,没有任何文件,而在VS2010中是有一个Web配置文件的。创建网站的时候,为了熟悉VS的应用,刚开始把VS中所有的新建的网站类型挨个试了一遍,比较了各自的不同,从而加深了印象。对于第一个编写的是简单的登录页面

simplelogin.ashx中的代码是

<%@ WebHandler Language="C#" Class="Simplelogin" %>

using System;
using System.Web;

public class Simplelogin : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        string modelPath = context.Server.MapPath("Loginmodel.htm");
        string SendBack = System.IO.File.ReadAllText(modelPath);      
        context.Response.ContentType = "text/plain";
        context.Response.Write("htmlSendBack");
        if (!string.IsNullOrEmpty(context.Request.Form["txtName"]))
        {
            if (context.Request.Form["txtName"] == "zhangshijiao" && context.Request.Form["txtPwd"] == "123123")
            {
              context.Response.Write("登录成功了");
            }
            else
            {
                context.Response.Write("登录失败了");
            }
        }
    }
 
    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>简单登录</title>
</head>
<body>
<form action="Simplelogin.ashx" method="post">
<input type="text" name="txtName" />
<input type="text" name="txtPwd" />
<input type="submit" value="登录" />
</form>
</body>
</html>

 

,出的错误主要是代码写错。

第二个是获取服务器响应时的时间,代码简单,主要是为了练习.ashx的用法。就是在刚才建的网站中新建文件.02date.ashx:

<%@ WebHandler Language="C#" Class="_02GetServerDate" %>

using System;
using System.Web;

public class _02GetServerDate : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        DateTime serverTime = DateTime.Now;
        context.Response.Write("Hello World !"+ serverTime.ToString());
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

在.ashx中由IHttpHandler接口实现外部请求,也就是说没有接口就不能实现外部的请求。可以看出服务器的应用是把类的代码变成IHttpHandler的接口用ProcessRequest来重写这个类,从而由context.Response.Write将其返回给浏览器。

 

原创粉丝点击