Conclusion: It has been two or three years since the emergence of Asp.net MVC (version 2009 and later). However, there are still very few Chinese learning materials in this regard, especially Asp.net mvc3, there are almost no Chinese learning books. There are two classic mvc3 tutorials in English: Professional ASP. net MVC 3 by Jon Galloway, Phil haack, Brad Wilson, K. scott Allen and pro ASP. net MVC 3 framework by Steven Sanderson. I am reading the next book. Although I barely understand it, I hope that it will be helpful to anyone who is not willing to read the English version of mvc3 just like me, so record the content learned every day. There will certainly be a lot of deficiencies. please correct me!
This article: I have just read the first three chapters. Below is a summary of the content of the first three chapters.
Chapter 1: Describes the stages of web development, mainly the development of Asp.net versions. The emergence of Asp.net seems to have translated the winform development method into web development. Microsoft has provided us with a large number of server controls. The emergence of a large number of controls shields us from the stateless HTTP, even HTML is automatically generated for us. So Asp.net is easy to get started with, but it is very difficult to get started with it. Since the development of traditional webforms, many defects have been exposed, and the emergence of Asp.net MVC has been well solved. Webforms has the following Disadvantages:
1. The viewstate problem causes repeated back-and-forth transmission in the browser and server, which affects the page rendering efficiency.
2. Page lifecycle: Part of the page lifecycle: The mechanism for connecting client events and server event processing code is very complex. Only a few developers can fully master and control it.
3. The focus cannot be broken down. MVC always implements the idea that conventions are better than configurations, and the focus is broken down.
4. Restricted HTML control. We know that the server-side control is finally presented in HTML format, but it is often not necessary and cannot be well applied. For example, if ID is changed, the ID rendering mode is set in 4.0.
5. Leaky extract action: literal translation is an abstract leak. I don't know how to translate it properly. What I understand is that the abstraction is incomplete. webforms tries to hide HTTP and HTML details wherever possible. When we implement our own logic, we often tend to deviate from the abstract, and therefore force us to reverse engineer the re-sending mechanism, or very clumsy to generate our expected HTML. In addition, all these abstractions will be a burden for component developers.
6. Weak Testability
Before Asp.net MVC, with rails, Asp.net MVC not only inherits the advantages of rails, but also expands and has more convenient functions.
Compared with Asp.net mvc1.0 and 2.0, a view engine razor is added to Asp.net mvc3.0, and the code block mark is changed from <%> @, the new tag symbol @ makes it easier to write and compile, and also facilitates unit testing.
The content in Chapter 2 is basically the environment configuration before development. We need to install Asp.net mvc3 separately, and vs2010 comes with MVC2.
The third chapter is a simple example. I will share the operation steps and Code as follows:
First, create a new mvc3 Project
Select
After the creation, the default project structure of Asp.net mvc3 is displayed.
At this time, when we run the program, Error 404 will occur, because we have not added any controller yet, add the first controller:
In the MVC Architecture, controllers is used to process requests. In Asp.net MVC, controllers are simple C # classes (generally Inheriting System. web. MVC. controller base class), each public method in controllers is called Action method, which means you can call it through URL. MVC Convention: Place All controllers in a folder named controllers (MVC is better than configuration). When I create an MVC project, the development environment is automatically generated for us.
Add a controller, as shown in:
Modify the homecontroller class as follows:
public string Index()
{
return "Hello World";
}
At this point, the program can run without a 404 error. After running, "Hello World" will be displayed in the browser ". Routes (Routing) is mentioned here. MVC uses ASP. net routing system to determine how URLs are mapped to actual controllers and actions. When creating the MVC project, we have added the default route according to the format or conventions of the root directory:/controllers:/Home Action: Index. When we enter http: // localhost/or http: // localhost/home in the browser, we will get Hello world. Of course, we can customize routing rules in global. asax. CS. The above action only returns a string. To respond to an HTML page of the browser, we need to create a view.
Modify the index as follows:
public ViewResult Index()
{
return View();
}
Return a viewresult type object through the MVC view method. No parameters are added to view (), so the default view is returned. If you run the program at this time, the following error is reported:
From the above error page, we can find out the order in which MVC looks for the view. Because no view corresponding to the index is created, an error is reported. To create a view, right-click the index method and choose add view, for example:
Add the following code:
@{
Layout = null;
}
<!DOCTYPE html>
<title>Index</title>
<body>
<div>
Hello World(from the view)
</div>
</body>
Run again: the browser displays Hello World (from the view)
Summary: for the first time, our index action returns a string, and for the second time, viewresult. At this time, MVC will be told to execute a view and return HTML, but we didn't tell the browser which view to return here. We just wrote return view () in the index. As mentioned above, MVC implements the idea that conventions are better than configurations, here is a Convention: The view name is the same as the action name in the Controller. Here it is the index, so the HTML with the view name "Index" is automatically displayed to the browser. The return type of action is called actionresult. viewresult is used here, And redirectresult is used for redirection.
Well, I will take notes tomorrow. I am also a beginner in MVC. I certainly have a lot of inaccurate or wrong understandings. Please give me more guidance from the passing experts.
Good night!