ASP. NET mvc2.0 URL routing entry

Source: Internet
Author: User

This articleArticleIs a follow-up introduction to ASP. NET mvc2.0. This article will not be open from RoutingSource codeThe URL rewriting technology of ASP. NET will also be briefly introduced. This section briefly introduces the use of ASP. NET mvc2.0 URL routing technology.

URL routing is an important part of MVC.However, it is independent from the MVC project and is not open-source. It is extracted as an independent project of Microsoft.ASP. net mvc only references this DLL.Create an MVC web project and open global. asax. We can see that the system has added using system. Web. Routing reference for us. Routing is referenced by our project as a separate DLL. Microsoft has not opened the routing source.Code. Therefore, ASP. net mvc is open-source, but routing is not open-source!

First, let's talk about ASP. net url rewriting technology.One of the simplest URL rewriting technologies is in the global. asax file.

Protected void application_beginrequest (Object sender, eventargs E)
{

String originalurl=Request. url. pathandquery; // obtain the original URL request (fake request)

// Convert the URL request into a true one ....

Httpcontext. Current. rewritepath (newurl); // submit the URL-specific requestHttpcontext

}

For example: a fake URL address: http://www.xxx.com/news/sports-2009-1-1-201.html

We can see that the URL is actually a real URL, which does not necessarily have an HTML page such as sports-2009-1-1.html. If we rewrite this URL in the application_beginrequest event as follows:

Http://www.xxx.com/news/sports.aspx? Date = 2009-1-1 1-1 & id = 201 this is the real URL!

Now let's talk about the Routing Mechanism of ASP. net mvc.First, we must have the idea that the URL address of ASP. net mvc is the WebProgram. Create an ASP. net mvc web project. Then press f5. The page automatically jumps to the http: // localhost: Port Number/home/index page. Xuanjicang is still in global. asax. See the following code:

Public Static Void Registerroutes (routecollection routes)
{
Routes. ignoreroute ( " {Resource}. axd/{* pathinfo} " );

Routes. maproute (
" Default " , // Route name
" {Controller}/{action}/{ID} " , // URL with Parameters
New {Controller = " Home " , Action = " Index " , ID = Urlparameter. optional }, // Default Value
New String [] { " Mvcdemo. Controllers " }
);

 

 

 global.  registerroutes   A method named:   default route, in the format of 
http: // localhost: port Number/ controller / action
and added the default new {controller = " Home " , action = " index " }. So when we press F5 to start it,
the address bar is: http: // localhost: Port Number/
, it matches our route, if there is no specific value, it matches our default value.
the page will automatically jump to the http: // localhost: Port Number/home/index page
first write it here and go home for dinner! To be continued!
 
First recognized ASP. NET mvc2.0 first recognized ASP. NET mvc2.0 [continued]

ASP. NET mvc2.0 URL routing entry-instance

ASP. NET mvc2.0 URL routing entry

Introduction to ASP. NET mvc3 (I) Environment preparations

ASP. NET mvc3 introduction to the ASP. NET mvc3 Project (II)

ASP. NET mvc3 simple getting started Season 1 (3) Introduction to filter in Controller

Asp. net mvc3 simple start season (4) detailed request processing pipeline

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.