ASP. NET MVC4 Learning System III (Controller controllers)

Source: Internet
Author: User

Controller (Controllers)
In the context of the MVC schema pattern, the controller responds to the user's input (for example, the user taps the "Save" button) and coordinates the model, view, and (often) the data access layer. In an ASP. NET MVC program, the controller is the class that contains the method that is called when the routing framework processes the request.

The

controller action
     Controller class is nothing special, unlike anything else. NET classes are almost indistinguishable. In fact, the methods in the control shun class (called the operations in the controller) do the main work during the processing request. Often hear the controller and controller operation of the word, in this book is also called, in fact, the MVC model does not distinguish between the two. However, the ASP. NET MVC framework focuses on the controller operation because it contains the actual logic code that handles the request.
     For example, the Homecontronller class contains three operations: Index,about and contact. However, assuming that the default route pattern is {controller}/{action}/{id}, When a requested URL is/home/about, the routing framework determines that the request is handled by the about method in the HomeController class. The ASP. NET MVC framework then creates an instance of Homcontroller and executes the About () method.
     In this example, the About () method is very simple: it passes the data to the view through the ViewBag property, and then asp.net  the MVC framework to display a view named (about) by calling the view () method , this operation returns the result of an operation of type Viewresult.

Operation result
It is important to note that the controller's job is to tell the ASP. What to do next, not how. This communication process is accomplished by using +actionresult+s, which is the operation provided by the Controller. For example, when the controller decides how to display the view, it tells the ASP. Net-Viewresult to show the view by returning it instead of rendering the view itself. This loosely coupled design is also the direct embodiment of the principle of "separation of concerns" in operation.
Although the operations of each controller are returned to ActionResult, most of the time you do not need to do it manually. Instead, you only need to use the help methods provided by the System.Web.MVc.Controller base class, for example:
Content (): Returns the Contentresult of the file type, such as "Hello, world!".
File (): Returns the contents of the type of Fileresult, such as PDF.
Httpnotfound () returns the Httpnotfoundresult that contains the 404HTTP status code.
JavaScript (): Return Javascriptresult

ASP. NET MVC4 Learning System III (Controller controllers)

Related Article

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.