1. Introduction
Create a new MVC project and add home and about two controllers
Add the index page to the two controllers
namespacestudy_mvc_route.controllers{ Public classHomecontroller:controller {//Get:home PublicActionResult Index () {returnView (); } Public stringRegex (intYearintMonthintDay ) { return$"{Year}-{month}-{day}";
$ is the inline parameter in the string }}}
namespace study_mvc_route.controllers{ publicclass aboutcontroller:controller { // get:about Public actionresult Index () { return View ();}} }
Under the App_start folder, the RouteConfig.cs is the MVC routing configuration file
The main attributes are as follows:
Name: route name URL: a regular expression that matches a URL, with {} representing a parameter variable, which can be empty; no {} is a hard match requirement defaults: By default, if the URL parameter is empty after matching the URL, the default action is used Controller = default, action = Default action, id = parameter
Constraints:url parameter constraints
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingSystem.Web.Routing;namespacestudy_mvc_route{ Public classRouteconfig { Public Static voidRegisterRoutes (routecollection routes)//route set, matched by top down routing{routes. Ignoreroute ("{Resource}.axd/{*pathinfo}");//Ignore route
//--------------------------------Add a route------------------------------------------ //URL to start with test
URL not introducedController parameter, so the controller uses the default value, and if you do not assign a value to the parameter, use the default value
routes. MapRoute (Name:"Test", URL:"Test/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional});
//Route writing can omit named parameters, name, URL, defaults these named parameters can be omitted, as follows//URL for about can access Home/indexRoutes. MapRoute ("ABOUT1"," About",New{controller ="Home", action ="Index", id =urlparameter.optional}); //URL for about Access About/indexRoutes. MapRoute ("About2","{Controller}",New{controller ="Home", action ="Index", id =urlparameter.optional});
The larger brackets indicate the parameters, not the same as a normal string link, which is a hard match requirement
Comparison of the transfer parameters//using constraints to do parameter constraints//home/regex_2014_05_19routes. MapRoute (Name:"Regex", URL:"{Controller}/{action}_{year}_{month}_{day}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}, Constraints:New{year =@"\d{4}", month =@"\d{2}", day =@"\d{2}"}//@ "\d{4}" means input four digits, @ table cancel \ Translation Effect ); Common Communication Parameters//home/regex?year=2014&month=05&day=19routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); } }}
Mvc_route Layer Depth