RewriterConfiguration.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Web;
  3. using System.Web.Caching;
  4. using System.Configuration;
  5. using System.Xml.Serialization;
  6. namespace URLRewriter.Config
  7. {
  8. /// <summary>
  9. /// Specifies the configuration settings in the Web.config for the RewriterRule.
  10. /// </summary>
  11. /// <remarks>This class defines the structure of the Rewriter configuration file in the Web.config file.
  12. /// Currently, it allows only for a set of rewrite rules; however, this approach allows for customization.
  13. /// For example, you could provide a ruleset that <i>doesn't</i> use regular expression matching; or a set of
  14. /// constant names and values, which could then be referenced in rewrite rules.
  15. /// <p />
  16. /// The structure in the Web.config file is as follows:
  17. /// <code>
  18. /// &lt;configuration&gt;
  19. /// &lt;configSections&gt;
  20. /// &lt;section name="RewriterConfig"
  21. /// type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /&gt;
  22. /// &lt;/configSections&gt;
  23. ///
  24. /// &lt;RewriterConfig&gt;
  25. /// &lt;Rules&gt;
  26. /// &lt;RewriterRule&gt;
  27. /// &lt;LookFor&gt;<i>pattern</i>&lt;/LookFor&gt;
  28. /// &lt;SendTo&gt;<i>replace with</i>&lt;/SendTo&gt;
  29. /// &lt;/RewriterRule&gt;
  30. /// &lt;RewriterRule&gt;
  31. /// &lt;LookFor&gt;<i>pattern</i>&lt;/LookFor&gt;
  32. /// &lt;SendTo&gt;<i>replace with</i>&lt;/SendTo&gt;
  33. /// &lt;/RewriterRule&gt;
  34. /// ...
  35. /// &lt;RewriterRule&gt;
  36. /// &lt;LookFor&gt;<i>pattern</i>&lt;/LookFor&gt;
  37. /// &lt;SendTo&gt;<i>replace with</i>&lt;/SendTo&gt;
  38. /// &lt;/RewriterRule&gt;
  39. /// &lt;/Rules&gt;
  40. /// &lt;/RewriterConfig&gt;
  41. ///
  42. /// &lt;system.web&gt;
  43. /// ...
  44. /// &lt;/system.web&gt;
  45. /// &lt;/configuration&gt;
  46. /// </code>
  47. /// </remarks>
  48. [Serializable()]
  49. [XmlRoot("RewriterConfig")]
  50. public class RewriterConfiguration
  51. {
  52. // private member variables
  53. private RewriterRuleCollection rules; // an instance of the RewriterRuleCollection class...
  54. /// <summary>
  55. /// GetConfig() returns an instance of the <b>RewriterConfiguration</b> class with the values populated from
  56. /// the Web.config file. It uses XML deserialization to convert the XML structure in Web.config into
  57. /// a <b>RewriterConfiguration</b> instance.
  58. /// </summary>
  59. /// <returns>A <see cref="RewriterConfiguration"/> instance.</returns>
  60. public static RewriterConfiguration GetConfig()
  61. {
  62. if (HttpContext.Current.Cache["RewriterConfig"] == null)
  63. HttpContext.Current.Cache.Insert("RewriterConfig", ConfigurationSettings.GetConfig("RewriterConfig"));
  64. return (RewriterConfiguration) HttpContext.Current.Cache["RewriterConfig"];
  65. }
  66. #region Public Properties
  67. /// <summary>
  68. /// A <see cref="RewriterRuleCollection"/> instance that provides access to a set of <see cref="RewriterRule"/>s.
  69. /// </summary>
  70. public RewriterRuleCollection Rules
  71. {
  72. get
  73. {
  74. return rules;
  75. }
  76. set
  77. {
  78. rules = value;
  79. }
  80. }
  81. #endregion
  82. }
  83. }