国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

web.config 關于HttpHandlers 和HttpModules的使用實例【轉】

2024-07-21 02:28:05
字體:
來源:轉載
供稿:網友
creating httphandlers and httpmodules
introduction
asp.net allows you to extend its functionality in two main ways :
httphandlers
httpmodules
http handlers are special applications that typically handle files with certain extension. for example when you request a file with extension .asp iis routes it to asp processor. but what if i want to handle files with my own extensions say .bipin? http handlers allow us to do just that. now, you might be thinking what is the use of a new file extension? consider a case where you want to generate graphics on the fly. in such cases you will write an http handler that will do your task. note that http handlers are typically called for the specific file extensions they are designed for. if you have worked with isapi extensions in past you will find this concept very familiar.
http modules are similar to http handlers in that they allow you to tweak with the request and response. however, they are typically executed for every request irrespective of the file type. if you have worked with isapi filters before you will find this concept very familiar.

in this article we will see how to create http handlers and http modules and use them in your asp.net pages.

ihttphandler interface
in order to create http handler you have to create a class that implements ihttphandler interface. this interface has one method with following signature :
sub processrequest(context as httpcontext)
it also has one read only property with following signature :
public readonly property isreusable() as boolean

the processrequest method is used to do all of your processing. the context parameter provides access to various objects like request and response. the isreusable property tells whether another requests can use the same instance of http handler.

creating the class implementing ihttphandler
following is a class that implements ihttphandler interface.

public class myhttphandler     
implements ihttphandler     public sub processrequest(byval context as httpcontext)             implements ihttphandler.processrequest         context.response.write("hello world")     end     sub public readonly property isreusable() as boolean             implements ihttphandler.isreusable         get            return true         end get     end property end class
here, we are simply outputting a string "hello world" for each request handled by this http handler. you can perform any complex task as per your requirements.

configuring our http handler
after you create your http handler class you should configure your web application so that specific requests will be handled by the handler. to accomplish this you will modify web.config file as follows :

<httphandlers>
<add verb="*" path="hello.aspx"
type="samplewebapplication.myhttphandler,samplewebapplication" />
</httphandlers>

here, verb attribute indicates get, post or * (all). the path attribute  indicates the resource to be handled. in our case we have specific file hello.aspx. type attribute indicates the fully qualified name of the class and name of assembly respectively.

in case you have to handle different extension say *.bipin then in addition to configuring in web.config (as shown above) you also need to add the extension in iis. this allows iis to forward request for specific extension to asp.net processor which in turn forwards it to your http handler.

testing your http handler
in order to test your http handler simply add a page named hello.aspx in the project and run it in the browser. you should get "hello world" displayed in your browser.

ihttpmodule interface
in order to create a httpmodule you will first create a class that implements ihttpmodule interface. this interface provides following two methods that you must implement :
sub init(byval app as httpapplication)
sub dispose()
out of the above two methods the init() method is of our interest. this method receives an instance of httpapplication that represents the current application instance. you will attach various event handlers in this method as we will see later on.
creating the class implementing ihttpmodule
now, let us create a class that implements ihttpmodule interface. here is the complete code for the class :
public class myhttpmodule
    implements ihttpmodule

    public sub init(byval app as httpapplication)
    implements ihttpmodule.init
        addhandler app.beginrequest, addressof mybeginrequest
        addhandler app.endrequest, addressof myendrequest
    end sub

    public sub dispose() implements ihttpmodule.dispose

    end sub

    public sub mybeginrequest
    (byval s as object, byval e as eventargs)
        dim app as httpapplication
        app = ctype(s, httpapplication)
        app.response.write("hello begin request")
    end sub

    public sub myendrequest
    (byval s as object, byval e as eventargs)
        dim app as httpapplication
        app = ctype(s, httpapplication)
        app.response.write("hello end request")
    end sub

end class



note how we have used init() method to attach event handlers to application events. in our example we have set mybeginrequest method to handle beginrequest event of httpapplication and myendrequest method handles endrequest event. this will cause the every request to output "hello begin request" and "hello end request" at the start and end of the page respectively.
add module details in web.config
prior to using the module we just developed we must inform iis and asp.net abut it. the place to do that is web.config file. add following section to the file :
<httpmodules>
    <add type="samplewebapplication.myhttpmodule,
    samplewebapplication"
    name="myhttpmodule" />
</httpmodules>



the <httpmodules> section is used to publish information about our module. all the modules from this section are loaded by asp.net at run time. the type attribute specifies the fully qualified class name and assembly name respectively.
testing your http module
in order to test our module, create a test web form and put some controls on it. (remember that if you use grid layout our messages may not be displayed exactly at the beginning and end. for our testing switch to flow layout). now run the web form. you should see our messages at the top and bottom of the web f


  • 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。
  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 喜德县| 沙洋县| 绍兴市| 宁陕县| 扎赉特旗| 建昌县| 额尔古纳市| 彭泽县| 彭水| 河池市| 澜沧| 滨海县| 西昌市| 福建省| 南华县| 新巴尔虎右旗| 临沂市| 武陟县| 石屏县| 天峨县| 大渡口区| 泸水县| 江油市| 余庆县| 莱州市| 北安市| 阿合奇县| 仪陇县| 酉阳| 衢州市| 阿克陶县| 辽源市| 和田县| 磴口县| 浦城县| 潢川县| 嘉兴市| 麻江县| 白城市| 越西县| 杭锦后旗|