Component haacked.com
1. Introducing components into an MVC project
2. Configure route rules
Public Static voidregisterroutes (routecollection routes) {//routes. Ignoreroute ("{resource}.axd/{*pathinfo}"); //ignore the About page, you cannot put it behind Maproute, the method is not a route component provides is the method under SYSTEM.WEB.MVC//routes. Ignoreroute ("Home/about");routes. MapRoute ("Default",//Route name "{Controller}/{action}/{id}",//URL with Parameters New{controller ="Home", action ="Index", id = urlparameter.optional},//parameter Default value New{controller=@"^\w+", action=@"^\w+"} ); Routes. MapRoute ("Filter",//Route name "{Controller}/{action}/{id}",//URL with Parameters New{controller ="Filter", action ="Index", id = urlparameter.optional},//parameter Default value New{controller =@"^\w+", action =@"^\w+" } ); Routes. MapRoute ("Car", "Car/{make}/{pro}", New{controller="Car", action="Index", id=1} ); Routes. MapRoute ("Archive", "Archive/{date}", New{controller ="Blog", action ="Archive" }, New{date=@"^\d{4}-\d{2}-\d{2}" } ); Routes. MapRoute (" Book", "Book/add/{name}", New{controller =" Book", action ="ADD" }, New{httpmethod="Post"} ); Routes. MapRoute ("Product", "Product/{*value}",//* means match everything New{controller="Product", action="Index" } ); /*dictionary<string, object> defaultrout = new dictionary<string, object> (); Defaultrout["action"] = "Index"; defaultrout["id"] = 0; RouteValueDictionary defaultroutevalue = new RouteValueDictionary (defaultrout); RouteValueDictionary constrainroutevalue = new RouteValueDictionary (); constrainroutevalue["Controller"] = @ "\w+"; constrainroutevalue["id"] = @ "\d+"; Route route = new Route ("{controller}/{action}/{id}", Defaultroutevalue, Constrainroutevalue, New Mvcroutehandler ());*/ }
View Code
3. Write in Global.asax.cs code
protected void Application_Start ()
{
Arearegistration.registerallareas ();
RegisterRoutes (routetable.routes);
Set to True to indicate that URLs pointing to physical files on disk will also be URL routing processed, such as a normal HTML page
RouteTable.Routes.RouteExistingFiles = true;
RouteDebug.RouteDebugger.RewriteRoutesForTesting (routetable.routes);
}
MVC Route detection Component Routing Debugger