Sorry, I just saw that the name and e-mail used for posting is wrongly changed
with another profile.
By the way.
Well, I believe to have said all on the first post, but let me try to explain
it again. Maybe that I wasn't so clear as I mean.
This is an extract of the web.config file
<configSections>
<section name="mySection" type="MyCompany.MySection"/>
</configSections>
<mySection>
<Item name="Giuseppe"/>
</mySection>
Then ... to handle mySection I wrote a class that inherits from ConfigurationSection
in which is defined a metod Item that return
a class that inherits from ConfigurationElement. In this class you must define
each property of Item, otherwise compiler generate an error during the startup.
So here it is the class.
public class Item : ConfigurationElement
{
[ConfigurationProperty("name",
DefaultValue = ",
IsRequired = false)]
public string name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
}
Now you want to be sure that name is in a certain range if it is numeric
or that doesn't contains some invalid characters.
So in case like this you could use the XValidator class; then in case of
the string you probably use the stringValidator, right?
But StringValidator perform a RegEx search, so if you want to match your
value with something that is in a list, this isn't suit your goal.
Your last chance is to implement your own Validator Class.
So this is what I do.
[AttributeUsage(AttributeTargets.Property, AllowMultiple=false)]
public sealed class NameAttribute : ConfigurationValidatorAttribute
{
public ContentTypeAttribute()
{
}
public override ConfigurationValidatorBase ValidatorInstance
{
get
{
return new NameValidator(""); // Here there is a problem
}
}
}
public class NameValidator : ConfigurationValidatorBase
{
String[] ct = new String[] {"Andrea", "Giuseppe"};
public NameValidator (String name)
{
if (base.CanValidate(name.GetType()))
this.Validate(NameValidator);
}
public override void Validate(object value)
{
if (Array.IndexOf(ct, value) == -1)
throw new Exception("Value isn't allowed");
}
}
Basically it should be the same that a XValidator does, but there is one
thing that isn't so clear to me :
how the current value stored in the property Name of the Element Item arrive
to my validator.
Bye
Andrea
NS> Well, first of all, the web.config file is an XML file, so you won't
NS> be able to write VB.NET or C#.NET code in it to begin with. The
NS> web.config file is simply a list of settings, so I think I am a
NS> little confused about what you are planning to do with your
NS> validation code. If you simply want to write a class, create a .VB
NS> or .CS file (depending on what language you are using). If you
NS> simply want a function or sub that does the validation, you can
NS> either write a class containing nothing except that function or sub,
NS> or you could add the function or sub to the Global class (located in
NS> the Global.asax.vb or Global.asax.cs file). If you could try to
NS> clarify what you are planning on doing with this validator, it might
NS> help me and anyone else figure out what you are looking for. (NOTE:
NS> When posting a Reply, it is good to include the previous message so
NS> that people can see anything you have already tried, mentioned, as
NS> well as what the original problem was)
NS>
NS> "Giuseppe Esposito" <giusespo@no_spam_libero.it> wrote in message
NS> news:9f*************************@news.microsoft.co m...
NS>
Well, I need to use it in the web.config file, not in an asp page, so
I suppose that a CustomValidator doesn't suit my needs, does it?
ByeGiuseppe