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 }}