0
我正在嘗試使用自定義配置節,這是我成功完成了無數次的事情,但由於某種原因,今天它無法正常工作。在配置部分中的代碼是:ASP.NET 3.5自定義配置部分不工作
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; }
}
從web.config中的相關章節是:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="httpRedirector" type="Company.HttpRedirector.RedirectorConfiguration, Company.HttpRedirector"/>
</configSections>
<httpRedirector redirectToUrl="http://www.google.com" />
</configuration>
而且我嘗試使用的代碼在以下的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);
}
}
}
}
發生了什麼事情是配置對象成功回來,但所有標記爲「ConfigurationProperty」的屬性都設置爲null/type默認值,就好像它從未試圖映射值配置文件。在啓動過程中沒有例外。
任何想法這裏發生了什麼?
NEvermind。我是個白癡。我應該從屬性返回base [「propertyName」]。衛生署! – Chris 2009-02-02 18:38:49