大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> ASP.NET中的Http Handles

ASP.NET中的Http Handles

时间: 2021-07-31 作者:daque

一、对于http handles asp.net的乞求进程是鉴于一个弹道(pipeline)模子的,asp.net会把一切的http乞求(requests)都发送给这个弹道里的http组件(modules)。每个组件在接受到http乞求保守行少许相映的举措。当http乞求经过了一切的http modules步调后,将会被交由一个http handle步调来处置,处置后的截止又将经过弹道里http modules归来。这所有进程中,被挪用的http module不妨有多个,但是挪用的http handle只能是一个。其进程如图: 不妨看出每个输出的http乞求城市最后被一个http handle步调处置。http handle是一个实行了system.web.ihttphandler接口的类的范例,有些一致isapi扩充。在http handles中实行的有:processrequest:该本领用来处置http乞求,是http handles最中心的本领isreusable:一个属性,归来一个bool值,来表白这http handle的范例能否能被重用来处置多个同典型的http乞求。二、在摆设文献中备案http handles http handles的类不妨在web.config或machine.config文献里备案。如许,一旦有相映的http乞求输出,这个http handle类就会被范例化。在web.config或machine.config文献里咱们用<httphandlers>和<add>节点来为咱们的运用步调增添http handle类:<httphandlers> <add verb="supported http verbs" path="path" type="namespace.classname, assemblyname" /><httphandlers>在<add>里1、verb属性说领会该handle所扶助的http乞求办法,比方扶助post和get办法,verb属性则为"post,get";即使扶助一切的乞求办法,verb属性则用"*"。2、path属性说领会对哪些文献的乞求才挪用该handle来处置,比方你只想在乞求my.possible文献时才挪用该handle,则path属性为"my.possible",即使你想一切后缀名为possible的文献(*.possible)都由该handle来处置,则path属性为"*.possible"。3、type属性中指定了handle类的定名空间、类名和配件名(工程名)。asp.net runtime会开始到运用步调的bin目次下搜索该配件的dll,即使没有找到再到gac里搜索。本来asp.net自己里的很多功效也是运用http handlers来实行,asp.net运用了很多的handle类来处置.aspx, .asmx, .soap 和少许其它的asp.net文献。你不妨在machine.config文献里找到如次代码:<httphandlers><add verb="*" path="trace.axd" type="system.web.handlers.tracehandler"/> <add verb="*" path="*.aspx" type="system.web.ui.pagehandlerfactory"/> <add verb="*" path="*.ashx" type="system.web.ui.simplehandlerfactory"/> <add verb="*" path="*.config" type="system.web.httpforbiddenhandler"/> <add verb="get,head" path="*" type="system.web.staticfilehandler"/> . . . . . . . . . . . .</httphandlers>从上头的摆设不妨看出来对.aspx文献的乞求是交由system.web.ui.pagehandlerfactory类来处置的,而象.config文献是被system.web.httpforbiddenhandler类来处置,不妨估计这个类将会归来一个该类文献不许被乞求的缺点。三、http handles类的实行 在这边咱们用c#来创造一个新的handle类来处置新的文献典型,比方以.possible为后缀名的文献。1、咱们先在vs.net里创造web运用步调的工程,名为myhandler,而后增添一个类文献newhandler.cs来创造实行了ihttphandler接口的类:using system;using system.web;namespace myhandler{ /// <summary> /// summary description for newhandler. /// </summary> public class newhandler : ihttphandler { public newhandler() { // // todo: add constructor logic here // } #region implementation of ihttphandler public void processrequest(system.web.httpcontext context) { httpresponse objresponse = context.response ; objresponse.write("<html><body><br><br><center>hi,this is a test! ") ; objresponse.write("</center></body></html>") ; } public bool isreusable { get { return true; } } #endregion }}在processrequest本领的实行里,咱们不过大略的获得了httpcontext的httpresponse东西,并象存户端发送了少许html。在isreusable的实行里归来true,表白该handle类的范例不妨处置多个对.possible文献的乞求。提防:即使想在http handlers里运用session,那么还须要实行irequiressessionstate接口,而irequiressessionstate接口不过一个标记,不须要实行任何的简直本领,以是只须要把类声明改为:public class newhandler : ihttphandler,irequiressessionstate即可。2、翻开web.config文献,备案上头新创造的handle类:<httphandlers> <add verb="*" path="*.possible" type="myhandler.newhandler,myhandler"/></httphandlers>3、在iis中增添isapi扩充,将咱们的新后缀名.possible增添进去,简直进程为:iis--》选中“默许网站”点右键--》选“属性”--》“主目次”--》“摆设”--》点“映照”里的“增添”按钮--》在弹出对话框里点击“欣赏”按钮,采用aspnet_isapi.dll文献,并在扩充名里填possible,如次图所示:结果点击决定按钮。如许咱们就不妨在欣赏器里输出http://localhost/myhandler/xxx.possible,来挪用该handle了。固然咱们这边不过举了个大略例子,以是输出任何*.possible都是一律的功效。咱们不妨在newhandler的processrequest本领里先领会乞求的url,而后按照各别url作出各别的相映,比方跳转到各别的如实生存的aspx页面,这也恰是web安排形式中mvc形式在asp.net中经过front controller实行的中心局部之一,简直请见:http://msdn.microsoft.com/architecture/patterns/default.aspx?pull=/library/en-us/dnpatterns/html/desfrontcontroller.asp。

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.