configSections

V

V

Hi,

As far as I remember (from .Net 1.1) you also need to specify the type
of the custom section, and if your type is not one of the pre-defined
type, then you need to specify a custom configuration section handler
in the config file (which of course you need to develop ).

Regards,
Vaibhav
 
P

Peter Morris [Droopy eyes software]

Hi all

I added the following to my web.config immediately beneath the opening
<configuration> declaration of my ASP.NET V2 website:

<configSections>

<section name="urlrewrites" type="UrlRewrite" />

</configSections>



But when I try to add the following within the web.config section I get
errors listed below:

<urlrewrites>

<rule>

<url>/Users/(.*)/(.*)\.aspx</url>

<rewrite>/$2/User.aspx?UserName=$1</rewrite>

</rule>

</urlrewrites>



Error 45 Unrecognized configuration section system.web/urlrewrites.
D:\Data\MyEcoSpace\WebSite\web.config 22




Can someone please tell me why this is not working?



Thanks



Pete
 
P

Peter Morris [Droopy eyes software]

Hi
You have to write up the object ("handler") which looks at the xml you put
in the config file.

That's what the "type" is in my XML
<section name="urlrewrites" type="UrlRewrite" />

UrlRewrite is the class that handles the section. It is a class within my
APP_CODE folder, it isn't wrapped in a namespace or anything. So have I
just declared it incorrectly? Could you tell me how to declare it properly?

Thanks

Pete
 
S

sloan

One of mine looks like this:

<section name="EmailSettingsSectionName"
type="GranadaCoder.Email.Settings.EmailSmtpSettingsHandler,GranadaCoder.Emai
l.Settings" />

or .. if I put in what it is:

<section name="NameOfConfigSectionLaterInConfigFile"
type="MyAssemblyName.Namespace1.Namespace2.MyObjectName,MyAssemblyName" />

Usually, "MyAssemblyName" is the same thing as MyAssembly.dll , but it
doesn't have to map like that.

Make sure you put in a break point IN
public object Create
somewhere..
Because my experience has been that you cannot "step into" the Create Method
from the outside.
(Microsoft is using reflection to instantiate your custom handler class)

Usually you find a bug or two inside the handler you need to weed out.
 
P

Peter Morris [Droopy eyes software]

using System;
using System.Web;
using System.Xml;
using System.Xml.XPath;
using System.Configuration;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Xml.Xsl;
using System.Reflection;
using System.Runtime.CompilerServices;

public class UrlRewriter : IConfigurationSectionHandler
{
protected XmlNode _oRules = null;

protected UrlRewriter() { }

public string GetSubstitution(string zPath)
{
Regex oReg;

foreach (XmlNode oNode in _oRules.SelectNodes("rule"))
{
oReg = new Regex(oNode.SelectSingleNode("url/text()").Value);
Match oMatch = oReg.Match(zPath);

if (oMatch.Success)
{
return oReg.Replace(zPath,
oNode.SelectSingleNode("rewrite/text()").Value);
}
}

return zPath;
}

public static void Process()
{
UrlRewriter oRewriter =
(UrlRewriter)ConfigurationSettings.GetConfig("system.web/urlrewrites");


string zSubst =
oRewriter.GetSubstitution(HttpContext.Current.Request.Path);


if (zSubst.Length > 0)
{
HttpContext.Current.RewritePath(zSubst);
}
}

#region Implementation of IConfigurationSectionHandler
public object Create(object parent, object configContext, XmlNode
section)
{
_oRules = section;

// TODO: Compile all Regular Expressions

return this;
}
#endregion
}
 
P

Peter Morris [Droopy eyes software]

Hi Sloan

I compiled the handler into an assembly which I then registered in the GAC.
My reference in the web.config is now a strong one and I no longer get an
error about my web.config having errors.

I'd certainly prefer to just use a class within the web project!
<section name="EmailSettingsSectionName"
type="GranadaCoder.Email.Settings.EmailSmtpSettingsHandler,GranadaCoder.Emai
l.Settings" />

Is yours in a separate assembly then?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top