[Preface] starting from today, Xiao Jing is going to learn about pro ASP. NET mvc3 framework, a classic masterpiece. I have downloaded an electronic version. Here, it is only used as a learning record for future reference and to urge you to stick to it. It is not used for any commercial purposes.
If you want to master a development framework, it is not enough to look at it with your eyes alone. You must use it first to get familiar with it quickly. Now, start by creating an mvc3 project and learn the basic structure and development process of the MVC project step by step.
1. Create the first MVC project:
- After the mvc3 environment is installed, create an mvc3 Project (ASP. NET mvc3 Web Application ):
- There are three project templates: Empty templates and Internet applications.ProgramAnd intranet applications. Select the second type.
- View engine: in earlier versions, the aspx engine was used. mvc3 introduced a new razor engine, which is easy to use and easy to use. Therefore, we recommend that you use the razor engine.
- Create a unit test project: A test project is automatically created after the project is selected.
- Click OK. The project is successfully created and some files are generated automatically. The project solution is as follows:
- Run the project to preview the effect:
2. Create the first controller:
In the webform development mode, the URL corresponds to the directory where the aspx file is located, but in the MVC structure, the request is processed through the Controller. In ASP. net mvc, controller is generally a class inherited from system. Web. MVC. Controller and is usually placed in the controllers folder. Each public method in controller is called an action method, and the Action method can be called through a specific URL.
- Right-click controllers => Add => controller. The name must end with controller. Three controller templates are provided by default. We select an empty template.
Public class indexcontroller: Controller
{
Public actionresult index ()
{
Return view (); // return the view result with the same name as the action
}
}
After the controller is created, an action named index is added by default, and its return type is actionresult. Return view () is used for returned results. If no parameter is specified, the view with the same name as the action is returned. You can also specify the view name by passing a string parameter.
3. Add a view to display the page
- View search path: Run the following command before adding a view:
This error not only prompts that the view cannot be found by MVC, but also shows the order of view searching. The first index is the Controller name in the URL (excluding the suffix), and the second index is the action name. If no action name is specified, the default index is used, of course, this can also be customized in the Global File.
- Add View: In the index actionCodeRight-click the block and select add view. For example, click OK.
We can see that the file is automatically added ~ /Views/index. cshtml. The path format is ~ /Views/controller name (remove suffix)/action name. cshtml,. cshtml suffix indicates the C # View File Using the razor engine. If the aspx engine is used, the View File suffix is. aspx.
4. viewbag outputs Dynamic Data :
The viewbag object can be used to transmit data from the Controller to the view. It is an attribute inherited by the controller from the parent class. view the following from the Object Browser:
PublicDynamic Viewbag{Get ;}
MemberSystem. Web. MVC.Controllerbase
Summary:
Gets the Dynamic View data dictionary.
Returns:
The Dynamic View data dictionary.
Instance used:
- Assign a value to viewbag in controller:
Public actionresult index ()
{
Int hour = datetime. Now. hour;
Viewbag. Hello = string. Format ("{0}, welcomeRice rebates www.damifanli.com", Hour <12? "Good morning": "Good afternoon ");
Return view ();
}
- Then read the data passed by viewbag in the View:
@{
Viewbag. Title = "Homepage ";
}
<H2> @ viewbag. Hello </H2>
- The running effect is as follows:
So far, we have created the first mvc3 project and have a preliminary understanding of the basic structure of the mvc3 project.