1.ASP. NET and MVC routing request processing
1) Processing of ASP.
Request---------Response Request (HttpModule)--------processing request (HttpHandler)--------return the requested resource to the client after processing
2) Processing of MVC
Derived from ASP. Also follows the process of request, response, processing, and resource return to the client
The request-----UrlRoutingModule (Response routing request)-----routingtable (The routing table) (retrieves whether the request path matches the route in the routing table)-----generate a route map. Generates a route object (which tells you which controller, which action, and which parameter)-----Mvchandler processing-----Response back to the client
Note:
UrlRoutingModule is also inherited from the IHttpModule interface.
Mvchandler is also inherited from the IHttpHandler interface.
2. Routing parameters
1) First define the parameters in the route, in addition to the Controller and action, the rest of the placeholder should be considered as parameters, such as:
routes. MapRoute ("DEFAULT1",//Route name "{Controller}/{action}/page_{id}_{num}",//URL ID with parameters num is a parameter New{controller =" Book", action ="List" }, New{id =@"\d", num =@"\d" } );
2) You need to write the corresponding parameter in the method (the parameter name in the routing table is the same as the parameter name in the action, for example:
PublicActionResult List (int? Idint? num=1) { //calculates the total number of data, calculates the total number of pages, retains the current page, preserves the category ID intPageSize =4; intEndnum = ((int) Num-1) *pageSize; varsum = Bookshop. Books.where (p = P.categoryid = = (int) ID). ToList (); intSumTotal = Sum. Count ();//Total Data intPagetotal = Convert.ToInt32 (math.ceiling (SumTotal *1.0/pageSize));//total number of pages intNownum = (int) Num;//Current PageViewbag.sumtotal =SumTotal; Viewbag.pagetotal=Pagetotal; Viewbag.nownum=Nownum; Viewbag.typeid=ID; List<Book> listpage = bookshop. Books.where (p = P.categoryid = = (int) ID). (P =p.id). Skip (Endnum). Take (pageSize). ToList (); returnView (listpage); }
3) The order in which the routes are retrieved is generally followed from top to bottom
4) Constraints on routing parameters
routes. MapRoute ("DEFAULT1",//Route name "{Controller}/{action}/page_{id}_{num}",//URL ID with parameters num is a parameter New{controller =" Book", action ="List" }, New{id =@"\d", num =@"\d" }Constraints
);
5) How to increase the constraints of a route
A. Placing matching more routes at the top of a route search
B. Taking a special route as the last side of the routing table
3.URL routing parameter, NULL processing
1) When defining a method in a controller, when you need to change the value type to a nullable value type, simply add a question mark (?) after the value type. ) processing
2) in razor, output variables are enclosed in parentheses.
There is a mistake to guide, thank you!
8.MVC framework Development (URL routing configuration and URL route parameter null processing)