Built-in parameter constraints
While the automatic parameters validation provided by SEO Extensions may be more than welcomed in new web sites and applications, you may want to
adjust or turn it off for existing projects or for specific cases, in order to resemble the standard ASP.NET MVC routing behavior. The good news is
that the parameters validation system is completely configurable by way of the
TypeDefaultConstraintRegistry
class.
Whenever SEO Extensions needs to validate a route parameter for an incoming request, it checks the aforementioned
Constraint
property first, and, should it be missing, checks the
TypeDefaultConstraintRegistry
class to see if a default constraint
for the type has been specified there.
In fact, SEO Extensions comes with a set of built-in constraints, automatically registered for the most used types exposed by the .NET Framework, including
numeric types, booleans, GUIDs and enumerations.
If you need to get rid of the existing parameter validation logic for one of your route parameters, for example, you may want to set the
Constraint
property of its
Route.Param
attribute to
null
:
public class MyController : Controller
{
[Route.Action("~/foo")]
public ActionResult Foo([Route.Param(Constraint = null)] int bar)
{
return Content("Baz!");
}
}
Replacing the parameters validation logic for your entire web site, by the way, could easily be done in your application class (global.asax)
by calling the
Register()
static method of the
TypeDefaultConstraintRegistry
(or
the
Unregister()
static method to completely remove the validation logic for a given type):
Excerpt from the application class (global.asax.cs):
public static void RegisterRoutes(RouteCollection routes)
{
// ...
TypeDefaultConstraintRegistry.Register(typeof(UInt32), "\d+");
// ...
}
Moreover, SEO Extensions allows to easily add your own default constraints to be used during parameter validations! Should you have a custom
business class you need to pass as a route parameter, for example, you may easily define a custom constraint and register it for your whole
application, using the aforementioned
Register()
method. This way, the framework will automatically validate
your parameters before executing the target actions using your own type constraints.