8.MVC framework Development (URL routing configuration and URL route parameter null processing)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.