ASP. net mvc 3.0 (II): MVC concept and MVC 3.0 development environment

Source: Internet
Author: User

ASP. net mvc 3.0 (I): summary of the new features of MVC 3.0

ASP. net mvc 3.0 (II): MVC concept and MVC 3.0 development environment

ASP. net mvc 3.0 (III): first recognized mvc url ing rules Routing

ASP. net mvc 3.0 (4): I want to configure routing for MVC rules.

ASP. net mvc 3.0 (V): Start With Controller/Action

ASP. net mvc 3.0 (6): Create your view in MVC 3.0

ASP. net mvc 3.0 (7): The New razor engine of MVC 3.0

ASP. net mvc 3.0 (8): passing and saving your model in MVC 3.0

ASP. net mvc 3.0 (9): MVC 3.0 verifies your model

ASP. net mvc 3.0 (10): MVC 3.0 uses forms authentication

ASP. net mvc 3.0 (11): Use filter in MVC 3.0

ASP. net mvc 3.0 (12): MVC 3.0 uses custom HTML controls

ASP. net mvc 3.0 (13): MVC 3.0 prevents cross-site Request Forgery (csrf) Attacks

ASP. net mvc 3.0 (14): create a data table in the MVC 3.0 instance Series

ASP. net mvc 3.0 (15th): Sorting of tables in the MVC 3.0 instance Series

ASP. net mvc 3.0 (16): pages of table data in the MVC 3.0 instance Series

ASP. net mvc 3.0 (17): filtering data in tables of MVC 3.0 instances

ASP. net mvc 3.0 (18): Combined sorting, paging, and filtering of tables in the MVC 3.0 instance Series

ASP. net mvc 3.0 (19th): using open-source controls for MVC 3.0 instances to sort and pagination tables

Summary

MVC: model, view, Controller)

Model: The model object is an application component that implements the application data domain logic. Typically, Model Objects retrieve model statuses and store them in databases. For example, a product object may retrieve information from the database, operate on the information, and write the updated information back to the products table in the SQL Server database.

In small applications, models are usually separated in terms of concepts, rather than actually. For example, if an application only reads a dataset and sends it to a view, the application does not have a physical model layer and associated classes. In this case, a dataset acts as a model object.

Controller: The controller is a component that processes user interaction, uses the model, and finally selects the view to be presented to display the UI. In MVC applications, the view only displays information. The controller is used to process and respond to user input and interaction. For example, the controller processes the query string values and passes these values to the model. The model may use these values to query the database. That is, the program uses the Controller to receive commands from the browser, decide what to do with it, and return it to the user. That is, to obtain the data, and then bind the data to the business logic of the page control.

View: View is a component that displays the Application User Interface (UI. Generally, this UI is created with model data. The Edit view of the Products table is an example of a view that displays text boxes, drop-down lists, and check boxes based on the current status of the product object. Is our ASPX page. Note that this is An ASPX page that does not contain background code files. Mvc3.0 view supports multiple view engines. That is, aspx and cshtml

A Design Pattern of Model-View-controller in software engineering. (He is not just Asp.net MVC, but only one of his implementations.) His main design goal is to separate user interfaces from the logic layer (low coupling ), in this way, developers can better focus on the design and testing of the logic layer, so that the entire program has a clear architecture.

Demonstrate the relationship between MVC

 

 

The MVC pattern helps you create applications that separate different aspects of an application (input logic, business logic, and UI logic), while providing loose coupling between these elements. This mode specifies the location of each logic in the application. The UI logic is in the view. The input logic is in the controller. The business logic is in the model. When you generate an application, using this separation method can help you simplify the complexity, because it enables you to focus on one aspect of the application at a time. For example, you can focus on a view independent of business logic.

The loose coupling between the three main components of the MVC application can also promote parallel development. For example, a developer can work on the view, a second developer can work on the Controller logic, and a third developer can focus on the business logic in the model.

Comparison and Selection of Asp.net MVC and Asp.net webforms

Contact:

Winform-> Asp-> Asp.net web form-> Asp.net MVC
Asp.net web form inherits the winform idea and is an event-driven programming model. To implement event-driven, component models and applications sent back to the page are inseparable.
Asp.net MVC is a URL-driven programming model. Is the implementation of the interface construction mode MVC in Asp.net.

Asp.net MVC is not used to replace Asp.net webforms, but to provide a new development mode for web development. You can choose a development mode suitable for yourself or your team.

Microsoft developed the ASP. net mvc framework based on the following objectives:

1) clear division of labor at each layer and testability (TDD supported)

2) A highly scalable plug-in architecture

3) more friendly URLs, easier Seo optimization, and support for rest.

4) You can continue to use some features of the original Asp.net.

5) fully controllable HTML code.

Advantages of ASP. net mvc:

1) the interface code and business logic code are completely separated. In Asp.net web form, the interface code and the business logic Code are not completely separated, but the two are separated by hiding the code file, the binding between the UI and business logic is still clearly visible in the hidden code file.
2) code separation achieves low coupling. Clear code, easy to maintain.
3) simple. Compared with the Asp.net web form, there are no complicated server controls (such as the gridview), no event-driven, and replaced by HTML tags (which is encouraged by Microsoft) and URL-driven.

4) Enhanced controllability of HTML.

5) more friendly URLs facilitate Seo

6) The generated HTML page is simpler and clearer, because no viewstate exists.

7) Support for JS frameworks by default

 

Advantages of ASP. NET webforms:

1) provides an event model for HTTP, which can be easily used by any developer.

2) provides a large number of standard controls and third-party controls to make development easier.

3) The viewstate mechanism is used to simplify state management.

So how should we choose the two during development?

  TDD Control html Data-driver Like winforms Rad
MVC Yes Yes No No No
Webforms No No Yes Yes Yes

 

 

 

 

The ASP. net mvc framework has the following functions:

  • Separation of application tasks (input logic, business logic, and UI logic), testability, and test-driven development (TDD ). All core protocols in the MVC Framework are based on interfaces and can be tested using mock objects. Mock objects are simulated objects that mimic the behavior of actual objects in an application. You can perform unit tests on applications without having to run controllers in ASP. NET processes, which makes unit tests fast and flexible. You can use any unit test framework compatible with the. NET Framework.

  • Scalable and pluggable frameworks. ASP. net mvc framework components are designed to easily replace or customize them. You can insert your own view engine, URL routing policy, operation method parameter serialization, and other components. The ASP. net mvc framework also supports dependency injection (DI) and control inversion (IOC) Container models. Di allows you to inject objects into the class, rather than relying on the class to create the object itself. IOC specifies whether an object requires other objects. The first object should obtain the second object from an external source such as the configuration file. This makes testing easier.

  • ASP. NET routing is widely supported. ASP. NET routing is a powerful URL ing component that allows you to generate applications with easy-to-understand and searchable URLs. URLs may not contain file extensions, and are designed to support URL naming patterns that are well suited to search engine optimization (SEO) and symbolic State Transfer (rest) addressing.

  • You can use tags in existing ASP. NET pages (. aspx files), user controls (. ascx files), and master pages (. Master files) as view templates. You can use existing ASP APIs such as nested master pages, inline expressions (<% = %>), declarative server controls, templates, data binding, and localization. NET functions and ASP.. Net MVC framework.

  • Supports existing ASP. NET functions. ASP. net MVC allows you to use some functions, such as Forms authentication and Windows authentication, URL Authorization, membership and role, output and data cache, session and Configuration File status management, health monitoring, Configuration System, and provider Architecture.

 

Preface

 

After learning about the concept of MVC. We need to build an environment to learn about MVC 3.0.

 

You can log on to http://www.asp.net/mvcdownload to install

 

 

Or download the offline installation package ASP. net mvc 3 RTM of MVC 3.0.

 

 

 

You can also install vs 2010sp1 for school exit installation.

 

 

 

 

 

 

 

Learning MVC 3.0 project templates

 

 

After MVC 3.0 is installed, you can create the mvc3.0 project.

Select Project template and view Engine

Vs 2010 MVC 3.0 solution default

Based on the razor view engine:

 

Based on the aspx view engine:

 

By default, the MVC project includes the following folders:

 

  • App_data: The physical storage area of the data. This folder serves the same purpose as ASP. NET websites that use web forms pages.

  • Content. We recommend that you add content files, such as Cascading Style Sheet files and images, to this location. Normally, the content folder is used to store static files.

  • Controllers. We recommend that you store controllers here. The MVC framework requires that the names of all controllers end with "controller", such as homecontroller, logincontroller, or productcontroller.

  • Models, which is a folder provided for the class that represents the MVC web application's application model. This folder usually includes code that defines the object and the logic used to define interaction with the data storage. Generally, the actual model object is located in a separate class library. However, when creating an application, you can place the class here and move it to a separate class library at a certain time in the development cycle.

  • Scripts. We recommend that you store script files that support applications in this location. By default, this folder contains basic ASP. NET Ajax files and jquery libraries.

  • Views. We recommend that you store the view here. The view uses the viewpage (. aspx), viewusercontrol (. ascx), and viewmasterpage (. Master) files, as well as any other files related to the rendered view. In the views folder, each controller has a folder named with the Controller name prefix. For example, if the Controller is named homecontroller, the views folder contains a folder named home. By default, when the ASP. net mvc Framework loads a view \Controller nameSearch for the viewpage (. aspx) file with the requested view name in the folder. By default, the views folder also has a folder named shared, but this folder does not correspond to any controller. Shared Folders are used to store views shared between multiple controllers. For example, you can place the master page of a Web application in the shared folder.

 

In addition to the folders listed above, the MVC web application also uses the code in the global. asax file to set the default value of the global URL route, and uses the Web. config file

Configure the application.

 

Running project results

 

 

It can be seen that this template has integrated a simple ASP. NET application integrating simple permissions, templates and navigation.

 

Create an empty MVC 3.0 Project

 

 

The empty projects in mvc3.0 are empty except the basic layout page error page and the start page.

In this case, we need to add them as needed during actual operations.

If you run the project directly, an error is reported ....

 

You will find that the default start page is still in global. asax.

 
Publicstaticvoid 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 of the Parameter
);

}
 

 

We can see that the controller folder is empty and there are no words about home ???? No wonder an error is reported...

In fact, this is why the index method of homecontroller cannot be found in MVC routing rules ..

For more information about routing, see ASP. net mvc 3.0 (3): First-recognized URL ing rules

Now you can create a new controller named homecontroller.

 

After adding the controller, the index method is provided by default...

 

public ActionResult Index()
{
return View();
}

 

I can't see that the above Code returns an actionresult-type view, but we haven't created a view corresponding to it ..

So let him output a string to the page...

 

Code

 

public ActionResult Index()
{
return View();
}

 

Change

 

publicstring Index()
{
return"Hello World";
}

 

After running, a simple interface is displayed...

 

 

Summary

After talking so much about it, it seems completely nonsense. There is no practical sentence. But it doesn't matter. The bricks will be very fierce ..

Bricks come with nonsense.

Only with bricks can we make progress !!!

Next Preview

 

In the next section, we will start with routing to build our own MVC 3.0 Application step by step.

See ASP. net mvc 3.0 (3): first recognized URL ing rules Routing

 

To:Memories of lost youthHttp://www.cnblogs.com/lukun/

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.