ASP. net mvc (III)

Source: Internet
Author: User

The responsibility of the ASP. NET Routing module is to map incoming browser requests to the unique MVC controller actions.

When we do not use a route, request http: // server/application/Produt. aspx? Id = 4. The server requests the Produt. aspx page and carries a parameter named id.

When a route is used and the same request is sent, the Produt. aspx page may not exist. For example, for http: // server/application/Products/show/beverages requests, the route parser processes Products show beverages. If the route is based on the path rule server/applicaton/{area}/{action}/{category}, it will find that Productes is area, it is a controller, and action corresponds to show, there is no doubt that beverages is categrory, so that the page can be correctly displayed.

2. Configure Route

When a new ASP. net mvc application is created, the application is configured with Routing in two places.

2.1 web. config

The configuration file contains four code snippets related to routing: system. web. httpModules code segment, system. web. httpHandlers code segment, system. webserver. modules code segment and system. webserver. handlers code segment. Do not delete these code segments. If they are not, routing will no longer run.

2.2 Global. asax

It contains event handlers of ASP. NET application lifecycle events. This route table will be created in the starting event of the application.

The default route will not waste your time here, so easy. Next let's take a look at how to customize Routes

 

3. Routing Overview

In a route, place a placeholder in braces to define ({and }). When the URL is parsed, the symbols "/" and "." are parsed as a delimiter, and the values between the delimiters match the placeholder. The information in the route definition that is not in braces is used as a constant value.

 

Route definition

Example of matching URL

{Controller}/{action}/{id}

/Products/show/beverages

{Table}/Details. aspx

/Products/Details. aspx

Blog/{action}/{entry}

/Blog/show/123

{Reporttype}/{year}/{month}/{day}

/Sales/2008/1/5

{Locale}/{action}

/US/show

{Language}-{country}/{action}

/En-US/show

4. Custom Routes

In the Global. asax file, use custom route to modify the default route table.

4.1 location: the custom Routes should be placed before the default route.

4.2 Example

Routes. IgnoreRoute (, {controller =, action =, id = UrlParameter. Optional}, {controller =, action =, id = UrlParameter. Optional} Database. DefaultConnectionFactory = SqlConnectionFactory (RegisterRoutes

When the program is started and Application_Start (), register the route table information RegisterRoutes

,},{,,},{,RegisterRoutes

References: http://msdn.microsoft.com/en-us/library/cc668201.aspx

 

Related Article

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.