简单的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" onclick="test()" />? ? </div></body></html>

?

?
=====推荐去看http://www.cnblogs.com/linecheng/archive/2011/12/14/2287822.htmlhttp://www.tracefact.net/Asp-Net/Introduction-to-Http-Handler.aspx
源代码:MyHandler.rar
?