简单的http Handler入门与源代码

 

=====================handler代码======================================

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
 
namespace MyHandler
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler, IRequiresSessionState
    {
        #region IHttpHandler 成员
 
 
 
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Write("<h1><b>Hello HttpHandler</b></h1>");
            context.Session["Test"] = "这个是测试HttpHandler容器中调用Session";
            context.Response.Write(context.Session["Test"]);
        }
 
        public bool IsReusable
        {
            get { return true; }
        }
        #endregion
    }
}
 
 

 

=================================配置=========================

 

 
<?xml version="1.0"?>
 
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
 
<configuration>
  <!--这个是配置数据库的配置-->
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
 
  <system.web>
    <!--这个是汇编配置不用我们配置-->
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>
 
    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>
 
    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>
 
    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
 
 
    <!--add verb="*" path="*" type="MyHandler.MyFirstHandler, MyHandler"/-->
    <!--<urlMappings enabled="true">
         <add url="~/Default.aspx" mappedUrl="~/Handler1.ashx"/>
      </urlMappings>-->
    <!--<add verb="*" path="Handler2.ashx" type="MyHandler1.MyHandlerFactory, MyHandler"/>-->
 
 
    <!--
      httpHandlers配置,此配置有类似J2EE的servlet配置
 
    -->
    <httpHandlers>
      <!--namespace.handlerl类名,加上命名空间name-->
      <add path="MyHandler" verb="*" type="MyHandler.Handler1,MyHandler"/>
    </httpHandlers>
  </system.web>
 
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>


 

 
 
=====================页面代码===============

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="MyHandler.index" %>

<!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 runat="server">
    <title></title>
</head>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<body>
    <form id="form1" runat="server">
    <div>
        <script>
            function test() {
                //MyHandler是Web.config的path相对于servlet的url 
                $.post('MyHandler', function (data) {
                    $('#result').html(data);
                });
            }
        </script>
    </div>
    </form>
    <div id="result">
         
        <input type="button" name="submit" id="submit" value="submit" οnclick="test()" />
    </div>
</body>
</html>
 
 


 



 

 

 
=====推荐去看
 
 
源代码:MyHandler.rar

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值