PageRenderTime 6ms CodeModel.GetById 2ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSASPNETCustomHttpHandlerandModule/CustomHandlerandModuleProject/CustomHttpModule.cs

#
C# | 93 lines | 40 code | 7 blank | 46 comment | 2 complexity | c254561bd1599e1c49a98aa45a30ff78 MD5 | raw file
 1/****************************** Module Header ******************************\
 2* Module Name:  CustomHttpModule
 3* Project:      CustomHandlerandModuleProject
 4* Copyright (c) Microsoft Corporation.
 5* 
 6* 
 7* This module implements IHttpModule interface to write custom Http Module
 8* to send response for the extension with .aspx resource.
 9*
10*  
11* This source is subject to the Microsoft Public License.
12* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
13* All other rights reserved.
14* 
15* History:
16* 11/27/2009 11:16 AM Thomas Sun Created
17\***************************************************************************/
18#region Using directives
19using System;
20using System.Collections.Generic;
21using System.Linq;
22using System.Web;
23#endregion Using directives
24
25namespace CustomHandlerandModuleProject
26{
27    /// <summary>
28    /// Inherit IHttpModule 
29    /// </summary>
30    public  class CustomHttpModule : IHttpModule
31    {
32        /// <summary>
33        /// Implement the Init method
34        /// </summary>
35        /// <param name="application">Current HttpApplication object</param>
36        public void Init(HttpApplication application)
37        {
38            // Register BeginRequest event
39            application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
40            // Register EndRequest event
41            application.EndRequest += (new EventHandler(this.Application_EndRequest));
42        }
43
44        /// <summary>
45        /// Write content to Response object in BeginRequest event
46        /// </summary>
47        /// <param name="source">Current HttpApplication object</param>
48        /// <param name="e"></param>
49        private void Application_BeginRequest(Object source, EventArgs e)
50        {
51            // Get current HttpApplication object
52            HttpApplication application = (HttpApplication)source;
53            // Get current Context object
54            HttpContext context = application.Context;
55            // Get request file path
56            string filePath = context.Request.FilePath;
57            // Get request file's extension
58            string fileExtension = VirtualPathUtility.GetExtension(filePath);
59
60            // Write content if extension is .aspx
61            if (fileExtension.Equals(".aspx"))
62            {
63                context.Response.Write("<h1><font color=red>Beginning of Request: added by Custom Module</font></h1><hr>");
64            }
65        }
66
67        /// <summary>
68        /// Write content to Response object in EndRequest event
69        /// </summary>
70        /// <param name="source">Current HttpApplication object</param>
71        /// <param name="e"></param>
72        private void Application_EndRequest(Object source, EventArgs e)
73        {
74            // Get current HttpApplication object
75            HttpApplication application = (HttpApplication)source;
76            // Get current Context object
77            HttpContext context = application.Context;
78            // Get request file path
79            string filePath = context.Request.FilePath;
80            // Get request file's extension
81            string fileExtension = VirtualPathUtility.GetExtension(filePath);
82
83            // Write content if extension is .aspx
84            if (fileExtension.Equals(".aspx"))
85            {
86                context.Response.Write("<hr><h1><font color=red>End of Request: added by Custom Module</font></h1>");
87            }
88        }
89
90        public void Dispose() { }
91
92    }
93}