Use urlrouting in ASP. net mvc 3.0

Source: Internet
Author: User
Tags file transfer protocol

1. Introduction to urlrouting

URL (Uniform Resource Locator), a uniform resource locator, is a way to fully describe the web pages or other resource addresses on the Internet.

Generally, a URL consists of six parts in the following format:

 
Protocol: // hostname [: Port] [/path] [? Parameters] [# fragment]

URL description:

    • Protocol. It can be HTTP (Hypertext Transfer Protocol), FTP (file transfer protocol), and HTTPS (Secure Hypertext Transfer Protocol ).
    • Hostname Host Name. The host name or IP address of the server that stores resources on the Internet.
    • Port Number. This option is a positive integer smaller than 66536 and a communication port agreed by each server or protocol.
    • Path. Indicates the address of a directory or file resource in a web site.
    • Parameters parameter list. The parameter format is a key/value pair separated by =, and multiple parameters are connected.
    • Fragment information Fragment. Used to directly locate an anchor in the page.

2. Differences between urlrouting and urlrewrite

Urlrouting is a set of processing requests from URLsProgramThe ing rules between them map URLs to actions that can handle business requirements. Urlrouting is an independent class library system. Web. Routing. dll.

Urlrouting maps the URL to the action of the controller. The processing flow chart is as follows:

Urlrewrite maps URLs to specific file resources. The process flow chart is as follows:

3. Use and customize urlrouting rules in ASP. NET MVC

In the ASP. net mvc project, Visual Studio automatically defines a routing rule in the global. ascx file. Urlrouting is used to define how to process client requests. Each ASP. net mvc application must define at least one urlrouting to specify how the application processes requests. A complex application can contain multiple urlrouting.

1>. Global. ascx File

In ASP. net mvc, all routing rules are defined in the Global. ascx file by default. The global. ascx file in the newly created ASP. net mvc 3.0 ProjectCodeAs follows:

 1   Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. Web;
5 Using System. Web. MVC;
6 Using System. Web. Routing;
7
8 Namespace Web
9 {
10 // Note: For instructions on enabling IIS6 or iis7 Classic mode,
11 // Visit Http://go.microsoft.com /? Linkid = 9394801
12
13 Public Class Mvcapplication: system. Web. httpapplication
14 {
15 Public Static Void Registerglobalfilters (globalfiltercollection filters)
16 {
17 Filters. Add ( New Handleerrorattribute ());
18 }
19
20 Public Static Void Registerroutes (routecollection routes)
21 {
22 Routes. ignoreroute ( " {Resource}. axd/{* pathinfo} " );
23
24 Routes. maproute (
25 " Default " , // Route name
26 " {Controller}/{action}/{ID} " , // URL with Parameters
27 New {Controller = " Home " , Action = " Index " , Id = urlparameter. optional} // Parameter defaults
28 );
29
30 }
31
32 Protected Void Application_start ()
33 {
34 Arearegistration. registerallareas ();
35
36 Registerglobalfilters (globalfilters. filters );
37 Registerroutes (routetable. routes );
38 }
39 }
40 }

2>. Route class
The routecollection object is declared as a static attribute in the routes attribute of routetable. The routecollection object stores instances of the route class. A complete route instance must have attributes such as URL, default value, constraint, data key, and route handler.

 1   Public Routevaluedictionary constraints { Get ; Set ;}
2 Public Routevaluedictionary datatokens { Get ; Set ;}
3 Public Routevaluedictionary defaults {Get ; Set ;}
4 Public Iroutehandler routehandler { Get ; Set ;}
5 Public String URL { Get ; Set ;}

3> route class attributes

URL attributes
In the route class, the attribute URL is a string used to describe the URL format in the request. This string may not be completely an actual URL. It can contain some placeholders marked by {} and can be used to extract data from the URL. For example:

1 "{Controller}/{action}/{ID}"

The value of the {controller} parameter is used to instantiate a control object for processing requests. The value of the {action} parameter is used to indicate that the method in the Controller will be called to process the current request.

Defaults attributes

 
1 New{Controller ="Home", Action ="Index", Id = urlparameter. optional}

Constraints attributes

1 New{Controller =@"^ \ W +", Action =@"^ \ W +", Id =@"\ D +"}

4> Custom urlrouting rules

A. Paging

 routes. maproute (
" page " ,
" {controller}/LIST/page/{page} " ,
New {controller = " Home " , Action = " List " , page = urlparameter. optional },< br> New {page = @" \ D * " }< br>);
Public StringList (Int? Page)
{
ReturnPage =Null?"1": Page. tostring ();
}

 

4. Use routedebugger to debug urlrouting

Routedebugger is an independent class library, routedebug. dll, which can be downloaded from the Internet and used as follows:

1> Add routedebug reference;

2> modify global. ascx

  1   using  routedebug; 
2
3 protected void application_start ()
4 {< br> 5 arearegistration. registerallareas ();
6
7 registerglobalfilters (globalfilters. filters);
8 registerroutes (routetable. routes);
9
10 routedebugger. rewriteroutesfortesting (routetable. routes); /// Add routedebug
11 }

attachment: routedebug.rar

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.