[Translation] professional ASP. NET mvc3 (03)-Chapter 1: Getting Started (bottom)

Source: Internet
Author: User
Tags ruby on rails
3. Understand MVC applications Program Structure

After Visual Studio is used to create a new ASP. net mvc application, some files and directories are automatically generated for the project, as shown in figure 1-10 .. The ASP. net mvc project has six top-level directories by default, as shown in Table 1-1.

Table 1-1: Default top-level directory

Directory

Purpose

/Controllers

Controller class used to process URL requests

/Models

Class for displaying and operating data Business Objects

/Views

Used to place the location of the User Interface Template file that represents the output (such as HTML)

/Scripts

Place JavaScript library files and scripts (. JS)

/Content

Place CSS, image files, and other non-dynamic or JavaScript content

/App_data

Used to store the location of the data file to be read/written

What if I don't like this directory structure?

ASP. net mvc does not require this structure. In fact, developers divide applications into multiple projects during large-scale application development to facilitate management (for example, data model classes are often separated, in a single class library project ). However, the default project structure does provide a good directory Convention. You can use it to maintain a clear application focus.

Note the following files and directories. When you expand:

    • /Controller Folder: Visual Studio adds two controller classes-homecontroller and accountcontroller (Figure 1-11) to the project by default ).

    • /Views Folder: by default, you will find three subdirectories --/account,/home,/shared, and some template files (1-12) are added to the project ).
    • /Content and/sriptsfolder, you will find the site.css file used to control the overall HTML format, and the Javascript class library (1-13) that enables the Project to Support jquery ).

 

    • Mvcmusicstore. Tests project: You can see two classes that contain unit tests on the Controller class (see Figure 1-14 ).

These default files added by Visual Studio provide you with the basic structure of an available application. At the end of the file, they contain the home page, about the page, account logon, logout, and registration page, and error handling page.

ASP. net mvc and conventions

ASP. net mvc applications rely heavily on conventions by default. It allows developers to avoid configuration and customization that can be inferred based on conventions.

For example, when using MVC to process a view template, MVC uses a directory naming structure based on conventions. These conventions enable you to ignore the physical path when referencing a view in the Controller class. ASP. net mvc searches for the view template file in the \ views [controllername] \ folder of the project by default.

MVC is designed around some obvious conventions based on default values. Of course, it can be rewritten if necessary. This concept is often called convention over configure ).

Conventon over configureation)

Conventions because the concept of configuration was popular in Ruby on Rails several years ago, the main meaning is:

So far, we know how to build a webApplication. Then we will integrate these experiences into the framework so that we do not have to re-configure all the configurations every time..

By viewing the three core folders running the application, we can find this concept in ASP. net mvc:

    • Controllers
    • Models
    • Views

You don't have to set these folder names in the web. config file -- they will be there as agreed. This eliminates the need to write XML files like Web. config. For example, to clearly tell the MVC engine, "you can find my views in the Views path"-it is known. This is an agreement.

This isn' t meant to be magical. well, actually, it is; it's just not meant to be black magic-the kind of magic where you may not get the outcome you expected (and moreover can actually harm you ). ASP. net MVC's Conventions are pretty straightforward. this is what is expected of your application's structure:

This is not a God of God. But in fact it is. But it is not a magic-from this magic you may not get the expected results (In addition, it may hurt you ). The conventions in ASP. net mvc are very simple. The following will be your software structure:

    • Each controller class is namedControllerThe end, such as productcontroller and homecontroller, all exist in the controllers directory.
    • There is a separate view directory to store all views in the program.
    • The views used by controllers are all in the subdirectories of the Views home directory and named according to the Controller name (removedControllerSuffix ). For example, the view corresponding to productcontroller mentioned above exists in the/views/product folder.

All the UI elements that can be shared exist in a similar structure, but they are in the shared directory under the views folder. For more information about views, see Chapter 1.

Conventions simplify Communication)

You writeCodeTo express and communicate with each other. It mainly needs to be presented to two different audiences:

    • You must clearly and clearly transmit the command to the computer;
    • Developers are expected to be able to locate and read your code for subsequent maintenance, debugging, and scaling.

We have discussed how conventions over configuration principles help you efficiently express your intent to MVC. The agreement also allows you to clearly communicate with other developers (including yourself in the future. By following common conventions, MVC developers around the world can share common benchmarks in all programs, avoiding repeated descriptions of every aspect of the program structure. One of the common advantages of a software design pattern is that it establishes a standard language. Because ASP. net MVC applies the MVC pattern and includes some Conventions and Conventions, making it easy for MVC developers to understand, even in large projects, that they did not write it themselves (or do not remember to write it).

Iv. Summary

This chapter covers a wide range of fields. First, we will introduce how ASP. NET web framework and MVC software pattern can be integrated into a powerful system to build Web applications. You can see how ASP. net mvc has matured through two versions, and deeply introduces the features and highlights of ASP. net mvc 3. Based on these foundations, the development environment is installed and a simple mvc3 application is created. Finally, view the structure and components of the MVC 3 application. Starting from controllers in Chapter 2nd, we will have a deeper understanding of the details of these components in the subsequent chapters.

 

Note: The reference book professional ASP. NET mvc3 is only for learning and communication. Do not use it for commercial purposes.

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.