Keeping validation rules in web.config may grow it pretty long and it is not convenient to make changes there anymore. Developers can manage validation rules easily by using Enterprise Library Configuration tool and therefore there is no good reason to handle validation rules manually. In this posting I will show you how to load validation rules from external configuration files.
Creating file for validation rules
Create new XML-file to your project and name it as Validation.config. I suggest .config extension because IIS blocks all requests to configuration files. Run Enterprise Library Condiguration tool and insert some validation rules.
Save changes and close configuration tool.
Loading validation rules from external file
Now open the class where you handle validation logic. In my last posting ASP.NET MVC: Validating objects using Enterprise Library validation application block I gave you some idea about how to use validation in your application. There was class called Validator that took object and validated it. All validation errors are returned as array.
In Validate<T>() method all validation rules are read from web.config. We have to modify this method to make it read validation rules from Validation.config. Here is the class with modifications.
public static class Validator
public static ValidationError Validate<T>(T instance)
var errors = new List<ValidationError>();
var source = new FileConfigurationSource("Validation.config");
var validator = ValidationFactory.CreateValidatorFromConfiguration<T>(source);
var results = validator.Validate(instance);
foreach (var result in results)
var error = new ValidationError();
error.PropertyName = result.Key;
error.Message = result.Message;
Validation rules are now read from Validation.config file and your web.config is lighter than before. Enterprise Library made this move very easy – only couple of lines of code were modified. Keeping validation rules in separate file is also safer – you don’t have to touch web.config to make modifications to validation rules.