views:

1011

answers:

1

I'm trying to use a custom configuration section, something I've done numerous times before with success, but for some reason, today it's not working. The code for the configuration section is:

public class RedirectorConfiguration : ConfigurationSection
{
    [ConfigurationProperty("requestRegex", DefaultValue = ".*")]
    public string RequestRegex { get; set; }

    [ConfigurationProperty("redirectToUrl", IsRequired = true)]
    public string RedirectToUrl { get; set; }

    [ConfigurationProperty("enabled", DefaultValue = true)]
    public bool Enabled { get; set; }
}

The relevant sections from the web.config are:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="httpRedirector" type="Company.HttpRedirector.RedirectorConfiguration, Company.HttpRedirector"/>
    </configSections>
    <httpRedirector redirectToUrl="http://www.google.com" />
</configuration>

And I'm trying to use the code in the following HttpModule:

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Company.HttpRedirector
{
    public class HttpRedirectorModule : IHttpModule
    {
        static RegexOptions regexOptions = RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace;
        static Regex requestRegex = null;

        public void Dispose() { }

        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }

        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            var app = sender as HttpApplication;
            var config = ConfigurationManager.GetSection("httpRedirector") as RedirectorConfiguration;

            if (app == null || app.Context == null || config == null)
                return; // nothing to do

            if (requestRegex == null)
            {
                requestRegex = new Regex(config.RequestRegex,
                    regexOptions | RegexOptions.Compiled);
            }

            var url = app.Request.Url.AbsoluteUri;
            if (requestRegex != null || requestRegex.IsMatch(url))
            {
                if (!string.IsNullOrEmpty(config.RedirectToUrl))
                    app.Response.Redirect(config.RedirectToUrl);
            }
        }
    }
}

What's happening is that the config object comes back successfully, but all of the properties marked "ConfigurationProperty" are set to null / type defaults, as if it never attempted to map the values from the config file. There are no exceptions during the startup process.

Any idea what's going on here?

+1  A: 

Your configuration class doesn't have the properties correct. It should read:

    [ConfigurationProperty("requestRegex", DefaultValue = ".*")]
    public string RequestRegex
    {
        get
        {
            return (string)this["requestRegex"];
        }
        set
        {
            this["requestRegex"] = value;
        } 
    }
ajma