PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms 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
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Web;
  23. #endregion Using directives
  24. namespace CustomHandlerandModuleProject
  25. {
  26. /// <summary>
  27. /// Inherit IHttpModule
  28. /// </summary>
  29. public class CustomHttpModule : IHttpModule
  30. {
  31. /// <summary>
  32. /// Implement the Init method
  33. /// </summary>
  34. /// <param name="application">Current HttpApplication object</param>
  35. public void Init(HttpApplication application)
  36. {
  37. // Register BeginRequest event
  38. application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
  39. // Register EndRequest event
  40. application.EndRequest += (new EventHandler(this.Application_EndRequest));
  41. }
  42. /// <summary>
  43. /// Write content to Response object in BeginRequest event
  44. /// </summary>
  45. /// <param name="source">Current HttpApplication object</param>
  46. /// <param name="e"></param>
  47. private void Application_BeginRequest(Object source, EventArgs e)
  48. {
  49. // Get current HttpApplication object
  50. HttpApplication application = (HttpApplication)source;
  51. // Get current Context object
  52. HttpContext context = application.Context;
  53. // Get request file path
  54. string filePath = context.Request.FilePath;
  55. // Get request file's extension
  56. string fileExtension = VirtualPathUtility.GetExtension(filePath);
  57. // Write content if extension is .aspx
  58. if (fileExtension.Equals(".aspx"))
  59. {
  60. context.Response.Write("<h1><font color=red>Beginning of Request: added by Custom Module</font></h1><hr>");
  61. }
  62. }
  63. /// <summary>
  64. /// Write content to Response object in EndRequest event
  65. /// </summary>
  66. /// <param name="source">Current HttpApplication object</param>
  67. /// <param name="e"></param>
  68. private void Application_EndRequest(Object source, EventArgs e)
  69. {
  70. // Get current HttpApplication object
  71. HttpApplication application = (HttpApplication)source;
  72. // Get current Context object
  73. HttpContext context = application.Context;
  74. // Get request file path
  75. string filePath = context.Request.FilePath;
  76. // Get request file's extension
  77. string fileExtension = VirtualPathUtility.GetExtension(filePath);
  78. // Write content if extension is .aspx
  79. if (fileExtension.Equals(".aspx"))
  80. {
  81. context.Response.Write("<hr><h1><font color=red>End of Request: added by Custom Module</font></h1>");
  82. }
  83. }
  84. public void Dispose() { }
  85. }
  86. }