Click here to Skip to main content
15,894,343 members
Articles / Web Development / ASP.NET

Introducing ASP.NET Page Modules

Rate me:
Please Sign up or sign in to vote.
3.81/5 (10 votes)
15 Apr 2010CPOL3 min read 57.5K   360   48  
This article introduces the concept of Page Modules, which are similar to HTTP Modules but related to Page Life Cycle, and the need for them.
using System;
using System.Configuration;

namespace PauloMorgado.Web.Configuration
{
    /// <summary>
    /// Configures the <see cref="T:PauloMorgado.Web.Configuration.PageModulesSection"></see> modules.
    /// This class cannot be inherited.
    /// </summary>
    public sealed class PageModuleAction : System.Configuration.ConfigurationElement
    {
        private PageModuleEntry pageModuleEntry;
        private static ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();
        private static readonly ConfigurationProperty nameProperty = new ConfigurationProperty("name", 
                                                                         typeof(string), 
                                                                         null, 
                                                                         null, 
                                                                         new StringValidator(1), 
                                                                         ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty typeProperty = new ConfigurationProperty("type", 
                                                                         typeof(string), 
                                                                         string.Empty, 
                                                                         ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationElementProperty elementProperty = new ConfigurationElementProperty(new CallbackValidator(typeof(PageModuleAction), Validate));

        static PageModuleAction()
        {
            properties.Add(nameProperty);
            properties.Add(typeProperty);
        }

        private static void Validate(object value)
        {
        }

        internal PageModuleAction()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:PauloMorgado.Web.Configuration.PageModuleAction"></see> class using the passed parameters.
        /// </summary>
        /// <param name="type">A comma-separated list containing the module type name and the assembly information. </param>
        /// <param name="name">The module name.</param>
        public PageModuleAction(string name, string type)
            : this()
        {
            this.Name = name;
            this.Type = type;
            this.pageModuleEntry = null;
        }

        /// <summary>
        /// Gets the <see cref="T:System.Configuration.ConfigurationElementProperty"/> object that represents the <see cref="T:System.Configuration.ConfigurationElement"/> object itself.
        /// </summary>
        /// <value></value>
        /// <returns>The <see cref="T:System.Configuration.ConfigurationElementProperty"/> that represents the <see cref="T:System.Configuration.ConfigurationElement"/> itself.</returns>
        protected override ConfigurationElementProperty ElementProperty
        {
            get
            {
                return elementProperty;
            }
        }

        internal PageModuleEntry Entry
        {
            get
            {
                try
                {
                    if (this.pageModuleEntry == null)
                    {
                        this.pageModuleEntry = new PageModuleEntry(this.Name, this.Type, typeProperty.Name, this);
                    }
                    return this.pageModuleEntry;
                }
                catch (Exception exception)
                {
                    throw new ConfigurationErrorsException(exception.Message, base.ElementInformation.Properties[typeProperty.Name].Source, base.ElementInformation.Properties[typeProperty.Name].LineNumber);
                }
            }
        }

        /// <summary>
        /// Gets or sets the module name.
        /// </summary>
        /// <returns>The module name.</returns>
        [ConfigurationProperty("name", IsRequired = true, IsKey = true, DefaultValue = ""), StringValidator(MinLength = 1)]
        public string Name
        {
            get
            {
                return (string)base[nameProperty];
            }
            set
            {
                base[nameProperty] = value;
            }
        }

        /// <summary>
        /// Gets the collection of properties.
        /// </summary>
        /// <value></value>
        /// <returns>The <see cref="T:System.Configuration.ConfigurationPropertyCollection"/> of properties for the element.</returns>
        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return properties;
            }
        }

        /// <summary>
        /// Gets or sets the module type.
        /// </summary>
        /// <returns>A comma-separated list containing the module type name and the assembly information. </returns>
        [ConfigurationProperty("type", IsRequired = true, DefaultValue = "")]
        public string Type
        {
            get
            {
                return (string)base[typeProperty];
            }
            set
            {
                base[typeProperty] = value;
            }
        }
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paulo Morgado
Portugal Portugal

Comments and Discussions