在10月份最后一天留个位子先,文章后来补上.
惭愧惭愧,小小博记推到年后才想起写上一笔,如此,怎能进步呢,哎哎,技术囧途中的码农,苦啊。。。。。。。。。。
在此记录下工作中遇到的技术的点点滴滴:把一般处理页写成cs文件,封装到类库里面。
Demo 描述:一个aspx页面,前台用ajax法送请求到类库中的handler,handler处理完返回想要输出的内容到前台。
目的:
1. 项目目录结构:
2.前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script type="text/javascript" language="javascript"> function VerifyEmpty(sender) { if (sender==true) { var isExist=false; if(document.all.<%=txtInput.ClientID %>.value=="") { alert("Number is being required."); return false; } else { $.ajax({ type: "GET", async:false, url: "verify.axd", data: {"id":document.all.<%=txtInput.ClientID %>.value}, success: function(data, textStatus) { if (data=="no") { //get 'no':means have the same one in DB ,if 'yes',it means allow submit isExist=true; } } }); if(isExist) { alert("Have the same one ,please try another."); return false; } } } else { if(document.all.<%=txtComment.ClientID %>.value=="") { alert("Please input comments."); return false; } } } </script> </head> <body> <form id="form1" runat="server"> <div> Number:<asp:TextBox ID="txtInput" runat="server" Width="200px"></asp:TextBox><br /> Comments:<asp:TextBox ID="txtComment" runat="server" TextMode="MultiLine" Width="200px"></asp:TextBox><br /> <asp:Button ID="btnApprove" runat="server" Text="Approve" OnClientClick="javascript:return VerifyEmpty(1)" /> <asp:Button ID="btnReject" runat="server" Text="Reject" OnClientClick="javascript:return VerifyEmpty(0)" /> </div> </form> </body> </html>
3.VerifyHandler代码:
namespace HandlerLib { using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; public class VerifyHandler : IHttpHandler { #region IHttpHandler Members public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { string id=context.Request.QueryString["id"]; if (!string.IsNullOrWhiteSpace(id)) { //ToDo something you want to expect context.Response.Write("Yes"); } else { context.Response.Write("No"); } } #endregion } }
4.Web.Config注册类库中的Handler
<?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET application, please visit 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> <httpHandlers> <add verb="*" path="verify.axd" type="HandlerLib.VerifyHandler,HandlerLib" validate="false"/>注册Handler </httpHandlers> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <handlers> <add name="Verify" verb="*" path="verify.axd" type="HandlerLib.VerifyHandler,HandlerLib"/>注册Handler
</handlers> </system.webServer> </configuration>
说明:
把ashx页面封装到类库的步骤:
1.拷贝ashx里面的代码逻辑到自己定义的cs文件,(只要是实现了IHttpHandler接口的类)
2.在web.config文件注册自己定义的handler
<httpHandlers> <add verb="*" path="verify.axd" type="HandlerLib.VerifyHandler,HandlerLib" validate="false"/>注册Handler </httpHandlers>
verb: verb可以是"GET"或"POST",表示对GET或POST的请求进行处理。"*"表示对所有请求进行处理,这里是对GET请求进行处理。
path: path指明对相应的文件进行处理,"*.aspx"表示对发给所有ASPX页面的请求进行处理,这里单独对redirect.aspx页面进行处理。可以指明路径,如"verify.axd"。表明只对verify.axd文件请求进行处理。
type: type属性中,逗号前的字符串指明HttpHandler的实现类的类名(完整的类名),后面的字符串指明程序集名称,也可以理解为命名空间。
3.调用封装好的自定义handler
$.ajax({ type: "GET", async:false, url: "verify.axd",//这里只要写和web.config中定义的path的值保持一致就可以,不用指向类所在的完整路径。 data: {"id":document.all.<%=txtInput.ClientID %>.value}, success: function(data, textStatus) { if (data=="no") { //get 'no':means have the same one in DB ,if 'yes',it means allow submit isExist=true; } } });
4. 在Handler文件中家断点调试:捕获请求成功,
至此,把一个ashx文件完整的搬到类库中,别成功使用。
Demo 源码:http://files.cnblogs.com/2012-10-3/TransferHandlerToLib.rar