An awesome routing engine for ASP.NET MVC 3 and 4
Foo()action even in the event of a wrong parameter type, as with a non-numeric parameter like in the url
~/foo/abc:
public class MyController : Controller
{
public ActionResult Foo(int bar)
{
return Content("Baz!");
}
}
public static void RegisterRoutes(RouteCollection routes)
{
// ...
routes.MapRoute("MyDangerousRoute",
"{action}/{bar}",
new {
controller = "MyController",
});
// ...
}
public class MyController : Controller
{
[Route.Action("~/foo")]
public ActionResult Foo([Route.Param] int bar)
{
return Content("Baz!");
}
}
Constraintproperty of the aforementioned
Route.Paramattribute to the desired constraint value.
itemparameter 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!");
}