Introduction to ASP. net mvc (Part 1)

Source: Internet
Author: User
Tags visual studio 2010

ReferencesASP. net mvc Overview.

1. MVC Mode

The MVC mode is a software architecture model. It divides a software system into three parts: Model, View, and Controller ). The earliest MVC ModeTrygve ReenskaugProposed in 1974, yesXerox palodo Research Center(Xerox PARC) in 1980s as a programming languageSmalltalkA Software Design Model invented. The purpose of the MVC pattern is to implement a dynamic program design, simplify subsequent modifications and extensions of the program, and make reuse of a part of the program possible. In addition, this mode simplifies the complexity to make the program structure more intuitive. The software system also gives the functions of each basic part through the separation of its basic parts.

Model)The data Model is used to encapsulate data related to the application's business logic and to process the data. The "model" has the right to directly access data, such as accessing databases. "Model" does not depend on "View" and "controller", that is, the model does not care about how it is displayed or operated. However, data changes in the model are generally published through a refresh mechanism. To implement this mechanism, the views used to monitor this model must be registered on this model in advance, so that the views can understand the changes in the data model.

View)The view layer can display data purposefully (theoretically, this is not necessary ). Generally, there is no program logic in the view. To refresh a view, the view needs to access the data Model it monitors. Therefore, you should register the data that it monitors in advance.

Controller)The Controller acts as an organizational unit between different layers and is used to control application processes. It processes events and responds. "Events" include user behaviors and changes in data models.

In the initialJSPWebpage, likeDatabaseData-Layer Code such as query statement and ImageHTMLSuch presentation layer code is mixed together. Experienced developers will separate the data from the presentation layer, but this is usually not easy. It requires careful planning and continuous attempts. MVC strictly separates them. Although constructing an MVC application requires some additional work, the benefits it brings us are beyond doubt.

First, multiple views can share one model. Today, the same Web application provides multiple user interfaces.EmailYou also want to accessEmailThis requires the Web site to provideInternetInterface andWAPInterface. In the MVC design mode, the model responds to user requests and returns response data. The view is responsible for formatting and presenting the data to the user. The business logic is separated from the presentation layer, the same model can be reused by different views, which greatly improves code reusability.

Second, the controller is a self-contained object, which is highly independent from the model and view. Therefore, the data layer and business rules of the application can be easily changed. For exampleMySQLPortOracle, OrRDBMSData Source changedLDAPData source, you only need to change the controller. Once the controller is correctly implemented, whether the data comes from the database orLDAPServer and view will display them correctly. Because the three modules in the MVC mode are independent of each other, changing one of them will not affect the other two. Therefore, based on this design idea, we can build good components with little mutual interference.

In addition, the Controller improves application flexibility and configurability. Controllers can be used to connect different models and views to meet user needs. They can also build applications to provide powerful means. Given some reusable models and views, the controller can select an appropriate model for processing based on the user's needs, and then select an appropriate view to display the processing results to the user.

2. ASP. net mvc 3 for Hello world

If no development environment is availableHereDownload Visual Studio 2010 and mvc3. create an MVC3 project and select the Razor template engine.

VS has created a basic directory structure and two default pages.

View the code of the file. You can see that the class in Controllers is used to process some logic processes, and the View is returned to generate the page. The code in the Model represents data and some basic verification rules. The View is filled with the data in the Model. Run the program to view a basic website. The MVC website runs like this:

1. when the first request is initiated from the client, the first request is Global. the Application_Start () method in asax to complete initialization. The important step is the RegisterRoutes method. This method specifies how to map URLs to specific methods.

2. Generate An aspx "target = _ blank> Based on the ing table specified in step 1.RouteDataObject To createRequestContextObject.

3. vc. mvcroutehandler (v = VS.98). aspx "target = _ blank>MvcRouteHandlerCreateMvcHandlerAnd pass the RequestContext object to MvcHandler.

4. The MvcHandler object uses the RequestContext object to determineIControllerFactoryObject To create a Controller object.

5. The MvcHandler object calls the Execute () method of the Controller object.

6. The Controller's ControolerActionInvoker object determines which action method the controller calls.

7. The Action method accepts user parameters, executes the method, and returns an object of the Result type.

Right-click the Controller folder, create an empty Controller, name it HelloWorld, and change the code to the following:

Html # viewSource "commandName =" viewSource "highlighterId =" highlighter_278856 "> view sourceprint?
01    
02 public
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.