In MVC, the Controller and Action are explained in the previous article.

Source: Internet
Author: User

In MVC, the Controller and Action are explained in the previous article.

Generally, it takes three steps to develop a program using mvc,

Create a model, a controller, and a view

Previously, all the development programs were developed in this step, and I have never thought about the mvc principle. For example, how does route find the controller and how does the controller find the action, how is actionResult output to view,

This article explains controller and action based on what you have learned. As for route --> controller --> action, this section will be explained in a casual manner.

 

I. controller class execution Principle

When we create a new Controller, we will find that it inherits the Controller class, while the Controller inherits the ControllerBase. The definition of ControllerBase is as follows:

We found that the ControllerBase class inherits the IController interface, and the IControlle interface has only one Execute method, while the Execute method has been implemented in the ControllerBase class. Writing so many actually available sentences can be summarized.

In the controller, all request processing starts from the Execute method.

Note: The Controller factory maintains a list of Controller classes. This list class must meet one of the following conditions: the Controller must be directly or indirectly inherited.

IController interface. The Controller inherited by the Controller has inherited the ControllerBase class, And the ControllerBase class has inherited and implemented the IController interface.

We can make a demo to verify:

After inheriting the BasicController class of IController, the Controller factory can find and create its instance, so that a simple Controller class is finished. At that time, it was not suitable for actual projects.

 

2. Understand ActionResult

Generally, we simply implement an ActionResult In the Controller.

        public ActionResult Index()        {            return View();        }

Mvc will go to the Index view, but how does mvc do it? We can view the definition of ActionResult,

    public abstract class ActionResult    {        protected ActionResult();        public abstract void ExecuteResult(ControllerContext context);    }

We found that there is a Rewritable ExecuteResult method in the ActionResult. This method is the key,Once the Controller executes the ActionResult method, the ExecuteResult in the ActionResult class will be called ..

Let's make a demo to test it.

Public class CustomReditect: ActionResult {public string url {get; set;} // rewrite the ExecuteResult method public override void ExecuteResult (ControllerContext context) {context. httpContext. response. redirect (url) ;}} public class DefaultController: Controller {public ActionResult Index () {return new CustomReditect {url = "/Redir/Index"}; // here is the key }}

 

Finally, we will summarize the Execute and ExecuteResult methods:

I. All mvc requests start with the execute method.

Ii. All classes that inherit ActionResult start with ExecuteResult.

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.