03軟謀教育httphandler在part 1處理流程一文中我們了解求處_第1頁(yè)
03軟謀教育httphandler在part 1處理流程一文中我們了解求處_第2頁(yè)
03軟謀教育httphandler在part 1處理流程一文中我們了解求處_第3頁(yè)
03軟謀教育httphandler在part 1處理流程一文中我們了解求處_第4頁(yè)
03軟謀教育httphandler在part 1處理流程一文中我們了解求處_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

Part.1HttpHttp一些運(yùn)作原理。我們知道HttpIHttpHandler,一個(gè)是例來(lái)了解IHttpHandler,看看掌握這些原理的實(shí)際用途。IHttpHandler有沒(méi)有辦法來(lái)通過(guò)編碼的方式來(lái)操控一個(gè)Http請(qǐng)求?,F(xiàn)這一操作的一個(gè)主要的接口,就是IHttpHandler(另一個(gè)是IHttpModule)。應(yīng)該還記得第一節(jié)中我們提到過(guò)ISAPI,它根據(jù)文件名后綴把不同的請(qǐng)求轉(zhuǎn)交給不同的處理程序。但是仔細(xì)看看就會(huì)發(fā)現(xiàn):幾乎一大半的文件都交給aspnet_isapi.dll去處理了。很明顯,aspnet_isapi.dll不可能對(duì)每種文件采用同一種方式處理,那么C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG錄下的web.config文件。NOTE:NOTE:machine.configv2.0.50727machine.confighttpHandlers:<httpHandlers <addpath="*.axd"verb="*"type="System.Web.HttpNotFoundHandler"validate="True"/><addpath="*.aspx"verb="*"type="System.Web.UI.PageHandlerFactory"validate="True"/><addpath="*.ashx"verb="*"type="System.Web.UI.SimpleHandlerFactory"validate="True"/><addpath="*.asax"verb="*"<add<addpath="*.ascx"verb="*"type="System.Web.HttpForbiddenHandler"validate="True"/><addpath="*.config"verb="*"type="System.Web.HttpForbiddenHandler"validate="True"/><addpath="*.cs"verb="*"type="System.Web.HttpForbiddenHandler"validate="True"/><addpath="*"verb="GET,HEAD,POST"type="System.Web.DefaultHttpHandler"validate="True"/> 可以看到,在<httpHandlers>結(jié)點(diǎn)中將不同的文件類型映射給不同的Handler去處理,對(duì)于.aspxSystem.Web.UI.PageHandlerFactorycs是一個(gè)單一的HttpHandler,IHttpHandlerFactory用來(lái)做什么后面會(huì)說(shuō)明。publicpublicinterfacevoidProcessRequest(HttpContextcontext);boolIsReusable{get;}}由上面可以看出IHttpHandler要求實(shí)現(xiàn)一個(gè)方法和一個(gè)屬性。其中IHttpHandlerHttp的實(shí)例,一般來(lái)說(shuō),我把它設(shè)置成true。publicpublicclassCustomHandler:publicvoidProcessRequest(HttpContextcontext)}publicbool{get{returntrue;}}C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\目通配符擴(kuò)大范圍,也可以明確指定這個(gè)handler僅用于處理某個(gè)特定的文件(比如說(shuō):位于Bin目錄下的編譯過(guò)的程序集名稱。NOTE:NOTE:這個(gè)例子,以及下面的一個(gè)例子均來(lái)自于《MaximizingASP.NETRealWorld,Object-OrientedDevelopment》一書(shū):usingSystem;usingSystem;namespacepublicclassJpgHandler:publicvoidProcessRequest(HttpContextstringFileName=(context.Request.UrlReferrer.Host.IndexOf("")>0){context.Response.ContentType="image/JPEG";context.Response.ContentType="image/JPEG";}}}publicIsReusable{get{returntrue;}}}csc/t:library/r:System.Web.dll Step.3CustomHandler.dllBin<addpath="*.jpg"verb="*"type="CustomHandler.JpgHandler,CustomHandler"/>建文件后,VS會(huì)自動(dòng)在文件中添加如下的代碼:<%@<%@WebHandlerLanguage="C#"Class="Handler"usingSystem;publicclassHandler:IHttpHandlerpublicvoidProcessRequest(HttpContext{context.Response.ContentType="text/plain";context.Response.Write("HelloWorld");}publicbool{getreturn}}}<%@<%@WebHandlerLanguage="C#"Class="Handler"usingusingusingSystem.Text;usingusingpublicclassHandler:IHttpHandler,{publicvoidProcessRequest(HttpContextcontext)Bitmapbasemap=newBitmap(200,60);Graphicsgraph=Graphics.FromImage(basemap);Fontfont=newFont(FontFamily.GenericSerif,48,FontStyle.Bold,GraphicsUnit.Pixel);Randomr=newstringletters="ABCDEFGHIJKLMNPQRSTUVWXYZ";stringletter;StringBuilders=newfor(intx=0;x<5;x++)letter=letters.Substring(r.Next(0,letters.Length-graph.DrawString(letter,font,newSolidBrush(Color.Black),x*38,r.Next(0,15));}PenlinePen=newPen(newSolidBrush(Color.Black),2);for(intx=0;x<6;x++)graph.DrawLine(linePen,newPoint(r.Next(0,199),r.Next(0,59)),newPoint(r.Next(0,199),r.Next(0,59)));basemap.Save(context.Response.OutputStream,ImageFormat.Gif);context.Session["CheckCode"]=s.ToString(); }publicbool{get{returntrue;}}個(gè)接口,MSDN的解釋是這樣的:SpecifiesthatthetargetHTTPhandlerrequiresreadandwriteaccesstosession-statevalues.Thisisamarkerinterfaceandpublicpublicinterface<img<imgsrc="Handler.ashx"alt="圖片驗(yàn)證碼在瀏覽器中打開(kāi)RSSRSSaspxRss.aspxISAPIHttpHandlerrss作為后綴名的文件來(lái)實(shí)現(xiàn)Rss源,比如說(shuō)Article.rss?,F(xiàn)在我們就一步步來(lái)實(shí)現(xiàn)它:CreateCreateTableIntNotNotNullConstraint NotNotNullDefaultGetDate(),NotNull,NotConstraintpk_RssSamplePrimaryInsertIntoRssSample(Title,Author,[Description],usingSystem;usingSystem;usingSystem.IO;usingSystem.Web;usingnamespaceRssFeadsLib{publicclassRssGenerator{publicstaticstring{MemoryStreamms=newXmlTextWriterwriter=newXmlTextWriter(ms,null);SqlConnectionconn=newSqlConnection("DataSqlCommandcmd=newSqlCommand("select*fromorderbypubdatedesc",SqlDataReaderreader=cmd.ExecuteReader();,"writer.WriteElementString("description","Dedicatedtowhile MMMyyyy12:00:00tt"));}byte[]data=newbyte[ms.Length];ms.Seek(0,SeekOrigin.Begin);ms.Read(data,0,data.Length);return}}}Step.3.rss名文件的Http請(qǐng)求。publicpublicRSSHandler:IHttpHandler{publicbool{get{return}publicvoidProcessRequest(HttpContextcontext){context.Response.ContentType="text/xml";stringstr=RssGenerator.GetRSS();}NOTE:NOTE:因?yàn)檫@個(gè)類和命名空間位于App_Code中,這里就不需要再手動(dòng)編譯Asp.NetPart.5點(diǎn)擊“添加”,設(shè)置“可執(zhí)行文件”為這個(gè)文件是否存在,也不管請(qǐng)求的是Article.rss還是Sample.rss。Step.6改為:Article.rss(改成abc.rssIHttpHandlerFactory候,可以考慮實(shí)現(xiàn)IHttpHandlerFactory來(lái)完成這一過(guò)程。publicpublicinterfaceIHttpHandlerGetHandler(HttpContextcontext,stringrequestType,stringurl,stringpathTranslated);}GetHandler(ReleaseHandler.atomrssIHttpHandlerFactoryclassclasspublicIHttpHandlerGetHandler(HttpContextcontext,stringrequestType,stringurl,stringpathTranslated){stringpath=context.Req

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論