Create a Routing constraint (C #)
You can use routing constraints to restrict browser requests that match a particular path. You can use a regular expression to create a routing constraint.
For example, suppose you have defined routes as follows:
Listing 1-global.asax.cs
Routes. Maproute (
"Product",
"Product/{productid}",
New {controller= "Product", action= "Details"}
);
Listing 1 contains a route named product. You can use this Product route to map browser requests to Productcontroller, as follows:
Listing 2-controllers\productcontroller.cs
Using SYSTEM.WEB.MVC;
Namespace Mvcapplication1.controllers
{
public class Productcontroller:controller
{
Public ActionResult Details (int productId)
{
return View ();
}
}
}
Note: the Details () action receives a single parameter named ProductID. This parameter is an integer parameter.
The route defined in Listing 1 will matches any one of the URLs:
?/product/23
?/PRODUCT/7
Unfortunately, this route also matches the following URLs:
?/product/blah
?/product/apple