Easily learn the execution process of MVC4.0-6 MVC

Source: Internet
Author: User

MVC is consistent with the traditional Asp.net at the underlying layer. The process is as follows:

1) in global. asax, In the application_start () event of the mvcapplication object, call routeconfig. registerroutes (routetable. routes) to register routing rules.

2) In the routeconfig. registerroutes () method, the default rule is {controller}/{action}/{ID }.

A. modify the rules here when you have special requirements.

B. The default controller is homecontoller, and the default action is index. That is to say, when you directly access the site, the index action under homecontroller is navigated by default.

C. The content followed by action is mapped to a parameter named ID. For example, user/delete/1 will match the delete (int id) method of usercontroller.

D. action can be divided into [httpget] and [httppost], which correspond to http get and post methods. That is, the directly requested URL only matches the [httpget] method, and the [httppost] method is [httpget] by default. the post action usually has a model parameter. MVC automatically fills the data in the form into the model object according to the agreed rules. The rule of this Convention is that the form name and attribute name have a corresponding rule.

3) MVC is responsible for importing the execution process into an appropriate action according to the above rules, that is, a method of controller.

4) in the action, you can directly return a string, output it to the browser, or return it to the actionresult object. The object points to a view page and includes some attributes as the container for data transmission.

Actionresult usually calls the view (…) of the controller object (...) The method can also be obtained through the Redirect () method or redirecttoaction () method. However, the latter two methods are mainly used to jump to other controllers, because the latter method cannot pass the model object. Action to pass data to the view, you can use model, viewbag or viewdata containers. Among them, model is strongly typed, so it is the top priority.

5) in the view, the execution itself is executed first, and then the template page and subpage are executed, but the execution order is inconsistent with the output order of the page content. The content output after the template can be in front of the content first output on the page.

In the view, you need to submit data or jump to the page, the action of a controller is directed through route. The final principle of transmitting data to actions in view is to use http get and post methods (including Ajax). The post method uses the agreed rules mentioned above, fill in the data in the form to the model parameter object. In the action, you can access the model object passed in through parameters, or the querystring parameter or form set in the original request object. Of course, the model object takes precedence.

This article is complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.