Step by step ASP. NET MVC3 (7) -- Controller, Action, ActionResult

Source: Internet
Author: User

Please indicate reprinted address: http://www.cnblogs.com/arhat

In the previous chapters, we have explained the View knowledge. Although there is still a lot of View-related knowledge, it doesn't matter. We will explain it later, because some View knowledge should be used with Controllrer. So from this chapter, we will begin to explain the knowledge of Controller.

In chapter 2, we briefly understood the Controller and knew that it is the core of the entire MVC. Now let's take a closer look at the Controller.

In ASP. net mvc, Controller is responsible for interaction between all clients and servers. It can be said that Controller plays a core role. How can we better understand Controller? Let's learn from an example in our life. In our daily life, we assume that there are three roles: manufacturers, wholesalers, and retailers ". The three roles have their own roles. It is easy for everyone to figure out that "wholesaler" is an intermediate link. He gets the goods from the "manufacturer" and then sends the goods to the "retailer" for sale, as for how "retailers" sell these commodities, "wholesalers" do not care, and "wholesalers" sell the goods to which "retailers" do not care. In this example, we can combine it with MVC to understand that "manufacturer" can be regarded as a Model, "wholesaler" can be considered as a Controller, and "retailer" is the View. Then you can see that the Controller obtains data from the Model and delivers the data to the View, while the View is responsible for displaying the data. The display of the View is irrelevant to the Controller. I don't know the above example. Can you understand the relationship between Controller, Model, and View?
Let's take a look at the Controller structure. Controller itself is a class. There are many methods in this class. If these methods are of the pubilc type, we call these methods Action ). This write operation is the core of interaction between the client and the server.
In ASP. net mvc, when creating a Controller, this class must end with a Controller. For example, if we create a Home Controller, its class name must be "HomeController", and the class must inherit from the Controller. Next let's take a look at the next HomeController.

 

As we said just now, the public method in Controller is an Action. The Index method of the Controller is an Action method. From this Controller, we can see that the basic requirements of a Controller are:

1. It must be a public class.

2. The Controller must end with the Controller.

3. inherit from the Controller class provided by the system

4. For the Action method, the method must be public.

Generally, when creating a Controller, there should be a folder in the Views folder with the same name as the Controller. This is the constraint of ASP. net mvc. Of course, this View folder, such as Ajax, is not required under certain conditions.

The Index action method can be seen in two places that need attention. One is the type of returned value ActionResutl, and the other is the returned value View (). Let's take a look at the explanation of ActionResult: ActionResult is the result to be sent to the browser after the Action is run, and ActionResult is an abstract class in the help document, so obviously View () the Return Value of the function must inherit the ActionResult. The type returned by the View () function is ViewResult. Next we will focus on ViewResult
ViewResult is the most commonly used ActionResult in ASP. net mvc. It is used to return a standard View page. The View that is returned depends on the Action name. Generally, the View page with the same name as the Action is returned. This is why the Views/Home/Index. cshtml file is returned when the HomeController/Index method is executed.

Let's take a look at the definition of the View () function:

    ViewResult View(  ViewResult View(   ViewResult View(IView view,   ViewResult View( viewName,   ViewResult View( viewName,    ViewResult View( viewName,  masterName,  model);

We can see that the View () function has not been reloaded for eight times. View () is the default call method to return View files with the same name as the Action. What if I want to use the default view file? We can use the overload function View (String viewName.

For example, we create A new View File "A. cshtml" in Views/Home ". The content is as follows:

In this case, we will change the next Index method.

 

It is reasonable to say that the. cshtml file should be loaded when accessing HomeController/Index. Is that true? We can preview and find that the displayed result is indeed page.

Similarly, when previewing In the preview tool, you should open Views/Home/B. cshtml. However, when Views/Home/does not have B. cshtml, what will happen?

Page B is not found, but we also know the order in which the View function looks up the View. If there is no View/Home, find it in Views/Shared. Create a B. cshtml file in Views/Shared. Then, preview and view the result.

As for other View () overload functions, you can study them on your own. If you use them later, Let's explain them again. At the same time, this chapter mainly describes a subclass of ActionResult: ViewResult. ViewResult represents the view file, and the order of ViewResult in view searching. This helps us manage our view files during development.

This chapter ends now. In the next chapter, we will continue to explain other sub-classes of ActionResult.

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.