Parameters validation
While it handles the SEO routes of your web application, Cobisi SEO Extensions automatically (more details available below) checks the types of your MVC actions parameters and
refuses to process routes whose parameters do not match with the aforementioned action parameters types.
Unlike the standard ASP.NET MVC routes handling,
SEO Extensions refrains to execute actions with the wrong parameter types and, instead, tries to match the routes with lower priorities, if available,
or returns an HTTP 404 error (meaning the requested page was not found).
With the following standard ASP.NET MVC action and route setup, for example, the system would execute the
Foo()
action
even
in the event of a wrong parameter type, as with a non-numeric parameter like in the url
~/foo/abc
:
Excerpt from the controller class:
public class MyController : Controller
{
public ActionResult Foo(int bar)
{
return Content("Baz!");
}
}
Excerpt from the application class (global.asax.cs):
public static void RegisterRoutes(RouteCollection routes)
{
// ...
routes.MapRoute("MyDangerousRoute",
"{action}/{bar}",
new {
controller = "MyController",
});
// ...
}
SEO Extensions works in a different way and allows to easily define your routes while also ensuring their parameters are of the
right types. To take advantage of this behavior, the previous example could then be refactored to the following (please note that
the SEO route does not require an explicit registration within the application class, as before):
public class MyController : Controller
{
[Route.Action("~/foo")]
public ActionResult Foo([Route.Param] int bar)
{
return Content("Baz!");
}
}
In addition to that, SEO Extensions allows to explicitly set constraints for your parameters and support both traditional regular expressions
and
IRouteConstraint implementations (for more advanced use).
To do that, you have just to set the
Constraint
property of the aforementioned
Route.Param
attribute
to the desired constraint value.
Here is, for example, how to limit the
item
parameter with a simple regular expression, so that it can only accept values starting with the
part-
prefix followed by exactly three digits, as with urls like
~/cart/add/part-123
:
[Route.Action("~/cart/add")]
public ActionResult AddItemToCart(
[Route.Param(Constraint = @"part-\d{3}")]
string item
)
{
return Content("Thanks for your order, baby!");
}