コントロールをドラッグしないASP.NETの2-一般的なハンドラの基礎

3136 ワード

TestHandler1.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace webapp_test1
{
    /// <summary>
    /// TestHandler1      
    /// </summary>
    public class TestHandler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string username = context.Request["username"];
            string password = context.Request["password"];
            string html = "<html><head></head><body><form action='TestHandler1.ashx' method='get'>" 
                + "<input type='text' name='username' value='{username}'/></br><input type='password' name='password' value='{password}'>" 
                + "</br><input type='submit' value='  '></form><p>{msg}</p></body></html>";

            if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
            {
                string code = html.Replace("{username}", "");
                code = code.Replace("{password}", "");
                code = code.Replace("{msg}", "");
                context.Response.Write(code);
            }
            else
            {
                string code = html.Replace("{username}", username);
                code = code.Replace("{password}", password);
                code = code.Replace("{msg}", "    ");
                context.Response.Write(code);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

Html1.htm
<!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="TestHandler1.ashx" method="post">
name <input type='text' name="name" /> </br>
password <input type='text' name="password" /> </br>
<textarea> -----------fds---</textarea> 
</br>
isVIP<input type="checkbox" name="isVIP"  /> </br>
<ul>
<li><input type="radio" name="gender" value="male" /> </li>
<li><input type="radio" name="gender" value="female" /> </li>
</ul>
<select name="prov">
<option value="bj">Beijing</option>
<option value="xm">Xiamen</option>
<option value="sh">Shanghai</option>
</select>
<input type="submit" />
</form>
</body>
</html>