What ASP. net mvc must know !, Asp. netmvc

Source: Internet
Author: User
Tags what is asp

What ASP. net mvc must know !, Asp. netmvc

The origin of MVC:

Before the MVC mode, the presentation of the View Interface, the capture and response of user interaction operations, the execution of business processes, and the storage of data are all combined. This design mode is called an autonomous View.

This re-design model has three major drawbacks:

Therefore, in order to solve these problems, some people have adopted the principle of separation of focus and separated the View Interface, business logic, and UI logic, and adopt a reasonable interaction mode to minimize their dependence. This mode is MVC.

 

What is the MVC mode:

MVC represents Model, View, and Controller respectively. From the perspective of human-computer interaction, View will capture user operations and send them directly to the Controller. The Controller will take the initiative to complete the corresponding UI logic, however, if business functions are designed,

The Controller also calls the Model to complete the cooperation. After completing the corresponding UI logic, the Controller controls the original View or creates a new View as needed to respond to user operations.

 

What is ASP. net mvc:

In a word, ASP. net mvc is a Web application framework built on the ASP. NET platform based on the MVC pattern.

For details, the ASP. NET platform adopts a pipeline design with good scalability. The entire ASP. net mvc framework is built by customizing the HTTP module and HttpHandler components of ASP. NET.

Note: The Model in MVC is mainly used to maintain the application status and provide business functions, but ASP. the Model in net mvc is different from the Model. The latter is only the data bound to the View. The two are not the same thing. Pay special attention to the learning and understanding process and do not confuse them.

 

How ASP. net mvc runs:

When a user request is submitted, ASP. net mvc implements route resolution for the current request. The goal of the resolution is to find the desired Controller, activate it, execute the corresponding Action method, and finally return what the user needs.

That is to say, after ASP. net mvc receives a request, the primary task is to parse the request to obtain the corresponding Controller and Action name. How does it resolve the request? This requires learning about the Asp. Net MVC routing system.

ASP. net mvc Routing System:

What is a route?

For ASP. net mvc applications, requests from browsers always point to an Action method defined in a Controller type. The ing between the request URL and the Target Controller/Action requires routing.

How does a route work?

In RouteConfig of the App_Start folder of ASp. net mvc, there is a static function

        public static void RegisterRoutes(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 is a process of Route registration. The core of Route registration is to create a Route object based on the provided routing rules (such as routing templates, constraints, and default values, and add it to the global routing table.

What is a global route table? What does it do?

The global route table is composed of ASP.. NET. Each Route object in the Route table contains a routing template. The name of the Controller and Action is defined in the template as a placeholder, that is, {controller} {action }.

For each HTTP request, the routing system traverses the Route table and finds a Route object that matches the current request URL pattern, then, it is used to parse the route data with the Controller and Action name as the core.

What is the MapRoute () method?

This method adds a custom URL ing rule to the routing system. How to add it? This is based on the RouteCollection type attribute Routes in the RouteTable object in the system context. The implementation in the MapRoute () method is to generate a Route object based on the parameter and add the Route object to the server.

Simply put, this code implements the function of parsing the URL address in the format of {controller}/{action}/{id} to obtain the corresponding Controller and Action. If not, the default address is/Home/Index.

 

After obtaining the required Controller and Action names through the routing system, the next step is to activate the Controller object and execute the Action method.

Controller activation:

The Controller object is activated in the factory mode. There is an IControllerFactory interface in the factory that activates the Controller. This interface has the unique CreateController method, activate the corresponding Controller object based on the current request context and the Controller name obtained through route resolution. The Code is as follows:

 

Public interface IControllerFactory{    IController CreateController(RequestContext requestContext,       string controllerName);}

 

Action execution:

Action execution is mainly performed through the Execute method in the Controller's base class ControllerBase. If the returned type of the target Action method is ActionResult, you also need to Execute this ActionResult object to respond to the current request.

In the ASP. net mvc Framework, the execution in both cases is completed through the ActionInvoke object.

public interface IActionInvoker{   void InvokeAction(ControllerContext controllerContext,string actionName);}

 

The ActionInvoke object defines an interface IActionInvoker, which defines the unique method InvokeAction. The first parameter of this method is the context object of the current Controller, and the second parameter is the name of the Action to be activated.

 

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.