An initial knowledge of the pit _MVC and the MVC application Structure of ASP.

Source: Internet
Author: User
Tags representational state transfer jquery library

aps.net MVC talk [go] from MSDN Click to access MVC theory structure

The model-View-controller (MVC) architecture pattern divides an application into three main components: models, views, and controllers. The ASP. NET MVC Framework provides an alternative mode for creating an ASP. NET Web Forms pattern for Web applications. The ASP. NET MVC Framework is a highly testable, lightweight presentation framework that integrates existing ASP. NET features, such as master pages and membership-based authentication, as well as WEB forms-based applications. The MVC framework is defined in the SYSTEM.WEB.MVC assembly.

MVC is a standard design pattern that many developers are familiar with. Some types of WEB applications will benefit from the MVC framework. Some types will continue to use the traditional ASP. NET application pattern based on Web forms and postbacks. Other types of WEB applications will combine these two approaches, both of which are not included in each other. The MVC framework includes the following components:

    • Model. A model object is an application part that implements the application data domain logic. Typically, model objects retrieve the model state and store it in a database. For example, the Product object might retrieve information from the database, manipulate that information, and then write the updated information back to the Products table in the SQL Server database. In a small application, a model is usually a conceptual separation rather than an actual separation. For example, if an application reads only the dataset and sends it to the view, the application does not have a physical model layer and associated classes. In this case, the dataset acts as a model object.

    • View. A view is a component that displays an application user interface (UI). Typically, this UI is created with model data.

    • Controller. A controller is a component that handles user interaction, uses the model, and ultimately selects the view to render to display the UI. In an MVC application, the view displays only information, and the controller is used to process and respond to user input and interaction. For example, the controller processes query string values and passes those values to the model, which the model might use to query the database.

The MVC pattern can help you create applications that separate the different aspects of your application (input logic, business logic, and UI logic) while providing loose coupling between those elements. This mode specifies where each type of logic should be located in the application. The UI logic is in the view. The input logic is located in the controller. The business logic is in the model. By using this separation method when you build your application, you can help simplify it because it allows you to focus on one aspect of implementing your application at a time. For example, you can focus on views that are independent of the business logic.

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

When to create an MVC application

You must carefully consider whether to use the ASP. NET MVC framework or the ASP. NET Web Forms model to implement the Web application. The MVC framework does not supersede the Web Forms model; You can use either framework for Web applications. (If you have existing WEB forms-based applications, these applications will continue to work exactly the way they always do.) )

Before you decide to use the MVC framework or the WEB forms model for a particular site, weigh the advantages of the various approaches.

The ASP. NET MVC Framework has the following advantages:
    • By dividing the application into models, views, and controllers, it is easier to simplify the process.

    • It does not use view state or server-based forms. This makes the MVC framework particularly suitable for developers who want full control over the behavior of the application.

    • It uses a front-end controller pattern that handles WEB application requests through a single controller. This allows you to design an application that supports a rich routing infrastructure.

    • It provides better support for test-driven development (TDD).

    • It is ideal for Web applications supported by a large team of developers, and for web designers who need to tightly control application behavior.

Web forms-based frameworks have the following advantages:
    • It supports an event model that preserves state through HTTP, which is beneficial for developing line-of-business Web applications. Web Forms-based applications provide many of the events that are supported in hundreds of server controls.

    • It uses page Controller mode to add functionality to a single page.

    • It uses view state for server-based forms, which makes it easier to manage state information.

    • It is ideal for small teams of Web developers and designers who want to quickly develop applications with a large number of components.

    • In general, it is simpler for application development because components (Page classes, controls, and so on) are tightly integrated and typically require less code than the MVC model

    • Separation, testability, and test-driven development (TDD) of application tasks (input logic, business logic, and UI logic). All core contracts in the MVC framework are interface-based and can be tested using mock objects, which are mock objects that mimic the behavior of the actual objects in the application. You can unit test your application without having to run the controller in an ASP process, which makes unit testing fast and flexible. You can use any unit test framework that is compatible with the. NET Framework.

    • Extensible and pluggable framework. The purpose of designing ASP. NET MVC framework components is to make it easy to replace or customize them. You can insert your own view engine, URL routing policy, action method parameter serialization, and other components. The ASP. NET MVC Framework also supports the use of dependency injection (DI) and control inversion (IOC) container models. DI enables you to inject objects into a class rather than relying on a class to create the object itself. IOC Specifies whether an object requires additional objects, and the first object should get a second object from an external source such as a configuration file. In this way, testing will be much easier.

    • Extensive support for ASP. NET routing is a powerful URL mapping component that allows you to generate applications with easy-to-understand searchable URLs. URLs do not necessarily contain file extensions and are designed to support URL naming patterns that are well suited for search engine optimization (SEO) and representational state Transfer (REST) addressing.

    • Supports using tags from existing ASP. NET pages (. aspx files), user controls (. ascx files), and master pages (. master files) in markup files as view templates. You can use existing ASP. NET features such as nested master pages, inline expressions (<%=%>), declarative server controls, templates, data binding, localization, and so on in conjunction with the ASP. NET. NET MVC framework.

    • Support for existing ASP. ASP. NET MVC allows you to use features such as Forms authentication and Windows authentication, URL authorization, membership and roles, output and data caching, session and profile state management, health monitoring, configuration system, and provider architecture.

MVC framework and application structure URL routing

The ASP. NET MVC framework uses the ASP. NET routing engine, which provides the flexibility to map URLs to the controller class. You can define the routing rules that the ASP. NET MVC framework uses to evaluate incoming URLs and select the appropriate controller. You can also have the routing engine automatically parse the variables defined in the URL and have the ASP. NET MVC Framework pass values as argument arguments to the controller.

MVC Framework and postbacks

Instead of using the ASP. NET Web forms postback model to interact with the server, the ASP-e framework routes all end-user interactions to the controller class. This keeps the UI logic separate from the business logic and helps to improve testability. Therefore, the ASP. NET view state and ASP. NET Web Forms page life cycle events are not integrated with MVC-based views.

WEB Application MVC Project structure

By default, the project structure is as follows
  • App_Data, this is the physical storage area of the data. This folder works the same as it does in an ASP. In most cases, the database is not actually placed in this folder, and the database server and the Web server should be separated from the general project, and the database connected to the connection node should be configured in Web. config.

  • Apps: Usually we put the common classes referenced in the Web project here, such as user password encryption, pagination control code, user login information preservation, etc...

  • Controllers: This folder mainly contains XML files, the system needs to use XML information files and so on.

  • Content, it is recommended that you add contents files in this location, such as cascading style sheet files, images, and so on. Typically, the Content folder is used to store static files. Small projects can store user data files in this folder, and more should exclude user files from the project.

  • Controllers, it is recommended to store the controller in this location. The MVC framework requires that all controllers have names that end with "controller". For example, Homecontrollers here, the controllers end is not deleted, otherwise the route will not recognize the controller.

  • Models, which is the folder that is provided for the class that represents the application model of the MVC WEB application. This folder typically includes code that defines the object and defines the logic that is used to interact with the data store. In general, the actual model objects will be in a separate class library. However, when you create a new application, you can place the class here and then move it to a separate class library at some point later in the development cycle. For example, when using a three-tier architecture, the contents of the Models folder are actually entities within the domain layer.

  • Scripts, we recommend that you store script files that support your application in this location. By default, this folder contains the ASP. NET AJAX base file and the JQuery library.

  • Views, it is recommended to store the view in this location. Views use ViewPage (. aspx), Viewusercontrol (. ascx), and ViewMasterPage (. master) files, as well as any other files related to rendering the view. In the Views folder, each controller has a folder named after the controller name prefix. For example, if the controller name is HomeController, the Views folder contains a folder called Home. By default, when the ASP. NET MVC Framework loads the view, it looks for the viewpage (. aspx) file that has the requested view name in the Views\ Controller name folder. By default, the Views folder also has a folder named Shared, but the folder does not correspond to any controllers. The shared folder is used to store views that are shared between multiple controllers. For example, you can place the master page of a WEB application in a Shared folder.

    After MVC3 joined the Razor view engine. The suffix of the view is. cshtml, which is much more flexible than the previous ASPX, and there are not many controls that can be dragged, and the ASP. NET MVC Framework was designed to allow developers to design Web applications more deeply. So many aspects need to write their own ...

  • Global.asax file: The code in the file to set the global URL route defaults in this file in MVC3 you can also place some methods that are initialized when the site loads.

  • Web. config file: This file is an XML file that contains the basic configuration information of the site, such as the Connection database node, the relevant parameters of the site load, and so on.

An initial knowledge of the pit _MVC and the MVC application Structure of ASP.

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.