RewriterConfig.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Configuration;
  3. namespace SiteCore.URLRewriter
  4. {
  5. // Define a custom section containing a simple element and a collection of the same element.
  6. // It uses two custom types: UrlsCollection and UrlsConfigElement.
  7. public class UrlsConfig
  8. {
  9. public static UrlsSection GetConfig()
  10. {
  11. return (UrlsSection)ConfigurationManager.GetSection("RewriterRule");
  12. }
  13. }
  14. public class UrlsSection : ConfigurationSection
  15. {
  16. [ConfigurationProperty("urls", IsDefaultCollection = false)]
  17. public UrlsCollection Urls
  18. {
  19. get
  20. {
  21. return (UrlsCollection)this["urls"];
  22. }
  23. }
  24. }
  25. // Define the UrlsCollection that contains UrlsConfigElement elements.
  26. public class UrlsCollection : ConfigurationElementCollection
  27. {
  28. protected override ConfigurationElement CreateNewElement()
  29. {
  30. return new UrlConfigElement();
  31. }
  32. protected override Object GetElementKey(ConfigurationElement element)
  33. {
  34. return ((UrlConfigElement)element).VirtualUrl;
  35. }
  36. public UrlConfigElement this[int index]
  37. {
  38. get
  39. {
  40. return (UrlConfigElement)BaseGet(index);
  41. }
  42. }
  43. }
  44. // Define the UrlConfigElement.
  45. public class UrlConfigElement : ConfigurationElement
  46. {
  47. [ConfigurationProperty("virtualUrl", IsRequired = true)]
  48. public string VirtualUrl
  49. {
  50. get
  51. {
  52. return (string)this["virtualUrl"];
  53. }
  54. set
  55. {
  56. this["virtualUrl"] = value;
  57. }
  58. }
  59. [ConfigurationProperty("destinationUrl", IsRequired = true)]
  60. public string DestinationUrl
  61. {
  62. get
  63. {
  64. return (string)this["destinationUrl"];
  65. }
  66. set
  67. {
  68. this["destinationUrl"] = value;
  69. }
  70. }
  71. }
  72. }