ASP. net mvc Tutorial: understanding models, views, and controllers (4)

Source: Internet
Author: User
This article provides an advanced overview of ASP. net mvc models, views, and controllers. In other words, it explains "M" "V" "C" in ASP. net mvc ". After reading this article, you should understand ASP.. Net MVC, you should also know how ASP. net MVC application and ASP.. NET web form applications.

When an ASP. NET application is started for the first time, application_start () is called () Method In listing 1, this method is called the registerroutes () method, and the registerroutes () method creates the default route table.

The default route table has only one route Record It divides all inbound requests into three sections. The first section maps to the Controller name, the second section maps to the behavior name, and the last section maps to the parameter ID passed to the behavior.

URL:

/Product/details/3

This URL is parsed into the following three parts:

Controller = productcontroller

Action = Details

Id = 3

Note that the suffix "controller" is at the end of the controller parameter, which is just a quirk of MVC.

The default route includes the default values. The default controller is homecontroller and the default behavior is index. The default ID is an empty string, Thoughts How to resolve the following URL in response:

/Employee

This URL is parsed into the following three paragraphs:

Controller = homecontroller

Action = index

Id = ""

The request is routed to the index () behavior of the homecontroller class.

Understanding Controller

Controller controls users and ASP. NET MVC Applications The interaction method is determined by the Controller to return content to the user who sent the request.

The controller is a class. The ASP. net mvc sample application only contains a controller named homecontroller. VB in the Controller folder. The contents of homecontroller. VB are shown in List 2.

Listing 2 homecontroller. VB

Public Class HomeController

   Inherits System.Web.Mvc.Controller

   Function Index()

     ViewData("Title") = "Home Page"

     ViewData("Message") = "Welcome to ASP.NET MVC!"

     Return View()

   End Function

   Function About()

     ViewData("Title") = "About Page"

     Return View()

   End Function

End Class

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.