ASP. NET: notes when customizing httpmodule

Source: Internet
Author: User
Let's talk about httpmodule again today. Here is a detailed description: Because the module processes all requests, if you need to write content to the response in the module, you need to make some judgments based on the request type.
 
For example, in the following example, some text is input during beginrequest. However, if the request is an image, inputting the text will undoubtedly invalidate the image.
 
 
 Using System; Using System. Collections. Generic; Using System. LINQ; Using System. Web; Using System. IO; Using System. xml. LINQ; Namespace Webapp { /// <Summary>     /// The function of this module is to output a text segment at the top and bottom of the page.      /// Prepared by Chen xizhang      /// </Summary>      Public   Class Mymodule: ihttpmodule { # Region Ihttpmodule Member Public   Void Dispose (){} Public   Void Init (httpapplication context) {context. beginrequest + = (O1, E1) => { // Because the module processes every request, you need to skip this step when considering some file types (instance slices ). Httpapplication APP = (httpapplication) O1;If(App. Request. url. absolutepath. endswith ("Aspx")) App. response. Write ( "Text on the top <HR/>" ) ;}; Context. endrequest + = (O2, E2) =>{ httpapplication APP = (httpapplication) O2; If (App. Request. url. absolutepath. endswith ( "Aspx" ) App. Context. response. Write ( "<HR/> text at the bottom" ); // Read the current user and record his access events                  String Logfile = app. server. mappath ( "Log. xml" ); Xdocument Doc =Null ; If (! File. exists (logfile) {Doc = New Xdocument ( New Xelement ( "Logs" ));} Else Doc = xdocument. Load (logfile); xelement item = New Xelement ( "Item" , New Xattribute ( "User" , App. User. Identity. Name ), New Xattribute ( "Time" , Datetime. Now ), New Xattribute ( "Path" , App. Request. Path); Doc. Root. Add (item); Doc. Save (logfile );};} # Endregion }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.