Introduction to the outset
Speaking of ASP, I think the 1th question for WebForms developers is: Why use it? My understanding is that MVC is a more detailed framework, "detail controllable" means that your system is more refined. Embodied in the application. The advent of MVC is likely to cater to the increasingly sophisticated needs of web development. As I understand a large investment management e-commerce platform, the architecture used is ASP. The trend of MVC's future application in systems is likely to be a large e-commerce platform. Of course, WebForms is not "abandoned", in the rapid development, control reuse, WebForms still walk in front. Sometimes, as needed, we can mix these two technologies in the same project.
Environment Readiness: Windows 7 SP1 or above operating system + IE10 or above browser + Visual Studio 2013
Demo creation and catalog file introduction
1. Open VS2013 and create a new MVC5 project.
OK, select the MVC template.
The default generated project directory structure is as follows
Directory File Description:
Directory or file |
Content |
App_Data |
Storing data files |
App_start |
Contains 4 class files, namely, binding configuration, filtering configuration, routing configuration, start (authentication) configuration. |
Content |
Contains 3 CSS files, note that in addition to the SITE.CSS, the highlight is the default integration of the bootstrap css/html framework that is popular abroad and Twitter uses. |
Controllers |
Controller, which contains two class files representing two controllers, respectively. For more information on the controller, follow this series of articles. |
Fonts |
Font |
Models |
Two model (entity) classes |
Scripts |
JS folder, including Bootstrap.js and Jquery-1.10.2.js. |
Views |
View, which is the page folder. Contains the. cshtml file. For cshtml, follow-up will be introduced in detail. |
Global.asax |
System entry |
Packages.config |
Important, document the version information of the key technical framework in the system. |
How MVC Works
In most web frameworks (Asp,asp.net form,jsp,php,...) , the URL address is typically mapped to a specific physical file on disk, for example,/helloword/welcome.php or/hellowod/welcome.aspx, Usually there is a welcome.php or welcome.aspx file on the disk that corresponds to it. When an HTTP request to a Web application is sent to the server, the Web framework passes the processing of the request to the requested file, which typically generates the response content sent to the client by using HTML tags. This process can be understood as a "file request".
What does MVC do?
The MVC framework uses a different approach to map URL paths directly to server-side code, which is to map URLs directly to classes rather than to physical files. These mapped classes are referred to as "controller classes", and they can handle server requests, process user input and interact with users, execute application logic and data logic, and so on. The control class will invoke a separate "view" component to produce the HTML output for the request. This process can be understood as "controller-style requests".
The following diagram explains the "Controller-type Request":
Now we can do a test to verify the "Controller-type request". Right-click in the Controllers folder and create a new controller
Select MVC5 Controller
We named the new controller Helloworldcontroller and changed the default code (very simple, you can write your own ^_^):
Then rebuild the project. Run, in the browser, at the site address (I here is http://localhost:55952, your port may be different) after +/helloworld, the effect is as follows:
Similarly, after the site address +/helloworld/welcome, the effect is as follows:
With this simple example, I think you understand and have a preliminary impression of the "Controller-type request". (The first input address is also accessible without adding the index method name because the index method is the default entry.) )
In fact, the above access rules can be mapped to the corresponding controller classes and methods in the class, is defined in the RouteConfig.cs file under the App_start folder. This class has only one method
Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); }
This method defines the mapping rules and the routes that can be ignored (also known as the types of access addresses that can be ignored).
For more detailed information on URL mappings, we'll go into more detail in the next chapter.
Summarize
This chapter mainly makes an entry-level introduction to ASP. MVC5, and makes a preliminary discussion and verification of the operation Principle of MVC. Hopefully this will be helpful for children's shoes that are not yet in contact with MVC. This chapter begins with a series of discussions about the content of the next article.
In addition, we do not forget to give the words of our predecessors: if you are not interested in MVC, it is not related, this is an "optional" item. You know, MVC and WebForms are co-exist.
At last
If you are interested in MVC5, please keep your eye on it. The author very much hope you can "recommend" a bit, and wish the work smoothly.
ASP. NET MVC 5 Web Programming 1--Getting Started