First, create an MVC project
Open VS202, click: New Project, File--->web->asp.net MVC 4 WEB Application
Fill in the project name, solution name, and file location, then click OK, and the following dialog box appears:
In the template selection, Microsoft provided us with 8 templates, which have the following meanings:
- Empty: Is an empty project, the basic components and directory structure is still there.
- Basic: A minimal template, with basic folders, CSS,MVC the basic structure of the application.
- Internet applications: A commonly-used, rich template that includes the ability to manage the accounts (dependent on ASP. Membership), which can be run directly after creation.
- Internet applications: Windows accounts that are used just like Internet applications.
- Mobile apps: Templates specifically for mobile devices, including mobile visuals, touch controls, and AJAX-enabled navigation.
- WEB API: A template specifically for HTTP services.
In the view engine, there are two options:
Where ASPX is the same codebehind way as WebForm, Razor is an engine specifically provided for MVC, with its specific syntax, see the previous Razor Markup Language introduction.
Unit tests only support visual Studio unit test by default, and only in VS2012 professional and above, if it is 2010, you need to download and install NUnit and other extensions
After selecting those above, click OK to create an MVC application
II. structure of the MVC project
After you select an Internet application, the following project structure appears:
Controllers: Place the Controller class, MVC requires that all controller names must end with "controller".
Models: Placing data descriptions, manipulating classes, and business object classes
Views: Place The view, the name of the folder below must be the same as the corresponding controller name, such as: Views/account folder contains all the views used by the AccountController class
Scripts: Placing JavaScript scripts
Images: Placing Images
Content: Place CSS and other things besides JavaScript scripts, images
Filters: Placing the filter code
App_Data: Placing data storage files
App_start: Place the configuration file code, the program's routing rules and filtering rules are configured under this folder
Third, the attention point
1. When passing arguments to an action, the name of the argument must be the same as the parameter name of the action method (case-insensitive) in order to get the value of the argument.
2, each controller, in the views directory has a name (remove controller suffix) named folder, each view under this folder corresponds to a method (Action) in the controller, View name is consistent with method name
3. The default startup page of the MVC program is set in the RouteConfig.cs file under the: App_star folder.
The matching rules for routes are defined in the RouteConfig.cs file
Routes. MapRoute (
Name: "Default",
URL: "{controller}/{action}/{id}",
defaults:new {controller = "Demo", action = "razortest", id = urlparameter.optional}
);
URL: ' {controller}/{action}/{id} ' defines a routing rule.
Defaults defines the page (View) for which the start page is the Razortest method under Democontroller:
4. Each action method returns a ActionResult structure by invoking the view () method, which returns the view of the action method under this controller if the view () method does not specify the name, or you can specify the view name or view path for the To return a specific view.