1. Understanding models, views, and controllers
This articleArticleThe main purpose is to understand the three concepts of M, V, and C in ASP. net mvc.
Through this article, we will learn how to coordinate ASP. net mvc and ASP. net mvc Architecture and ASP. NET web formsProgramStructure and ASP.
Ii. Simple ASP. net mvc Program
The ASP. net mvc template of vs generates a very simple and complete ASP. net mvc program framework. Here we will use this program framework.
In vs2008, click "file"-"New Project" to open the new project dialog box. Select the language type on the left, ASP. net mvc web application in the template area, enter the project name and project location, and click OK.
Figure 1
When creating an ASP. net mvc application, the system will prompt you if you want to create a trial project. Here we do not create a test project.
Figure 2
After the ASP. net mvc application is created, Several folders and files are displayed in the solution manager window. Three of these folders are very eye-catching:Models, views, Controllers. Literally, you can guess the functions of these three folders, which are the folders for storing model files, view files, and control files respectively.
Expand the controllers folder and we will see a file named homecontroller. CS. Expand the views folder and we will see two subfolders home and shared. Expand the home folder and we will see the two files about. aspx and home. aspx (as shown in) on the page ). This file forms a simple ASP. net mvc program framework.
Figure 3
When you press F5 or click the debug dynamic button for the first time, a dialog box is displayed, as shown in, prompting whether to enable the debug switch in the web. config file. Click "OK" to continue running the program.
Figure 4
When we run the ASP. net mvc application, vs starts the project in the browser. This simple program framework only contains two pages: The index page and the about page. The index page is displayed by default during running. When we click the about us link, the about page is displayed.
Figure 5
HereNote the address bar of the browser. When we click the home hyperlink, the browser's address bar will change to/home. When we click the About Us hyperlink, the browser's address bar will change to/about.
However, when we close the browser and return to the vs development interface, we did not find the home and about page files in the program. Why?
3. a URL does not correspond to a page!(Original: Home of gray wormsHttp://hi.baidu.com/grayworm)
In traditional ASP. NET web forms applications or ASP programs, their URLs correspond to page files one by one. If I want to access the somepage. ASPX page, there must be a somepage. aspx file on the server's hard disk. Otherwise, the Error 404-page not found will be generated.
On the contrary, in ASP. net mvc, there is no correspondence between the URL path in the browser address bar and the file in the application. In fact, in ASP. net mvc applications, the URL address in the browser address bar corresponds to the action of the controller.
In the traditional ASP. in the. NET or ASP program, browser requests are mapped to files. in the. net mvc application, browser requests are mapped to the Controller action.
Traditional ASP. NET or ASP programs are content-centric, while ASP. net mvc applications are program logic-centric.
4. Understand URL Routing
The browser request is mapped to the corresponding action of the controller. This ing process is called URL routing. URL routing routes inbound requests to the corresponding controller action.
URL routing processes requests through the route table. The route table is written in the global. asax file. When the program runs for the first time, it is called to create a route table. By default, the global. asax file of MVC Code As follows:
Listing 1- Global. asax
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. MVC;
Using system. Web. Routing;
Namespace mvcapplication1
{
Public class globalapplication: system. Web. httpapplication
{
Public static void registerroutes (routecollection routes)
{
Routes. ignoreroute ("{resource}. axd/{* pathinfo }");
Routes. maproute ("default", "{controller}/{action}/{ID}", new {controller = "home", Action = "Index ", id = ""});
}
Protected void Application_start ()
{
Registerroutes (routetable. routes );
}
}
}
When an ASP. NET application is started for the first time, the application_start () method is called. This method calls the registerroutes () method and creates a default route table in the registerroutes () method.
By default, a route table contains only one route. This route splits all the inbound request URLs into three parts:
Part 1: map to the Controller name.
Part 2: map to the action name of the controller.
Step 3: map to the ID parameter of the Action Function
Example: URL/Product/details/3It is parsed into the following three parts:
Controller = productcontroller
Action = Details
Id = 3
The three parts of the route haveDefault Value.
The default controller is homecontroller.
The default value of action is index.
The default value of ID is a null string.
Now, remember these default values, and we will analyze the following URL address.
/Employee
The three sections after the URL floor is parsed are:
Controller = employeecontroller
Action = index
Id = ""
Finally, if we open the ASP. net mvc application without adding any URL address:
For example:Http: // localhost/Resolved:
Controller = homecontroller
Action = index
Id = ""
As you can see, this situation is routed to the index () action of the homecontroller class by default.
5. Understanding the control layer
In the MVC program, the control layer is used to respond to user requests. When a browser sends a request, the control layer receives the request and sends the processing result back to the browser.
A controller is a class. In ASP. net mvc program, the Controller folder contains a controller named homecontroller. CS. The homecontroller. CS code is as follows:
Listing 2-homecontroller. CS
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. MVC;
Namespace mvcapplication1.controllers
{
Public class homecontroller: Controller
{
Public actionresult index ()
{
Viewdata ["title"] = "home page ";
Viewdata ["message"] = "Welcome to ASP. net mvc! ";
Return view ();
}
Public actionresult about ()
{
Viewdata ["title"] = "about page ";
Return view ();
}
}
}
The homecontroller contains two methods: Index () and about (). These two methods are two actions. When the client sends a/home/INDEX request, the homecontroller. Index () method is triggered. When the client sends a/home/about request, the homecontroller. About () method is triggered.
Any public method of the controller is treated as the action of the controller. That is to say, all the public methods of the controller can be called in the URL address bar of the browser. Therefore, we should be careful with this design.
6. Understanding the view layer
Both index () and about () in the homecontroller class at the control layer return a view. The view contains the HTML Tag and document content to be sent to the client. A view is equivalent to a page of an ASP. net mvc application.
When creating a view, we need to set the view position correctly.
The returned view path for the homecontroller. Index () action is \ views \ home \ index. aspx.
The returned view path for homecontroller. About () is \ views \ home \ about. aspx.
Generally, if you want the Controller Action to return to the view, you need to create a sub-folder under the views folder, which should have the same name as the controller sub-folder. In the subfolders, we need to create a. aspx file, which should have the same name as the controller action. The code for the about. aspx view is as follows:
Listing 3-about. aspx
<% @ Page Language = "C #" masterpagefile = "~ /Views/shared/site. Master "autoeventwireup =" true "codebehind =" about. aspx. cs "inherits =" mvcapplication1.views. Home. About "%>
<Asp: Content ID = "aboutcontent" contentplaceholderid = "maincontent" runat = "server">
<H2> about us </H2>
<P>
Todo: Put <em> about </em> content here.
</P>
</ASP: content>
In the above Code, except for the first line, the rest of the content is standard HTML. We can modify the HTML content of the view as needed.
A mvc view is similar to ASP or ASP. NET web forms. We can embed the corresponding server code (C # Or VB. NET) in the HTML content of the view ). Use script code to control data display.
7. Understanding model layer
the control layer and model layer are discussed above. Next let's take a look at the model layer
the MVC model layer contains all the logic of applications beyond the control layer and view layer. The model layer should contain all business logic and database access logic . for example, if you want to access the database using LINQ to SQL, you should create a LINQ to SQL class (dbml file) in the models folder ).
the view layer should only contain the logic for generating the user interface. The control layer should only contain a very small number of program logic such as the returned view or action jump. All other things should be included in the model layer. ( Original: Home of the gray worm http://hi.baidu.com/grayworm )
generally, we should try to implement" thick "model layer and" extremely thin "control layer . The method at the control layer should contain as few program code as possible. If the control layer is "too thick", we should consider transferring some logic of the control layer to the model layer.
conclusion:
This article describes ASP in general.. Net MVC web application.
know how to route the URL address to different controller actions.
know how to coordinate with views to return user data.
know how the model layer includes data access logic and business logic.