MVC Learning Series-controller

Source: Internet
Author: User

    • The core of MVC is the controller, which plays a core role in the function.

1,) in the MVC class library, according to the URL, through the mvchandler into the MVC processing system,

2,) parse the corresponding route information,

3,) in Mvchandler's Begin_requesthandler function, the corresponding controller, and action, are obtained according to the URL.

4) A validation process is required before executing the Controller.execute,

5,) After executing the Exceute,

6,) in the Execute process need to include validation related processing, need to process validation, etc.

7,) Execute Execute processing related business information, return to view,

8,) after further verification of the information,

9,) ...

This is only a general implementation process, but in the real source code we find that every step of the process will contain a number of prepared

For example: According to the controller,action name, get to the corresponding controller, and action, how to execute?

1,) here gets the corresponding controller instance object by reflection, and then obtains the corresponding parameter from the ControllerContext context so that the corresponding action function parameter is filled in the reflection;

2,) Here also contains a cache place, when the first time a controller instance is found, the MVC framework automatically stores such instance information, so that the next time according to the Controller and action name, you can quickly find the corresponding instance.

    • Below we create an ASP. NET MVC3 project to feel how the controller is used.

I created a homecontroller and created a view (~/views/home/index.cshtml) for the controller;

I create another area (Max-admin), and then I have an area, which contains a homecontroller in the area, and I also add a view to the HomeController (~/views/home/ index.cshtml);

Debug Project: Page ran out of exception (multiple types were found that match the controller named ' Home '. This can happen if the route that services this request (' {controller}/{action}/{id} ') does not specify namespaces to sear CH for a controller, that matches the request.)

We can see from the project that Global.asax.cs contains:

1  //note:for instructions on enabling IIS6 or IIS7 Classic mode,2     //Visithttp://go.microsoft.com/?LinkId=93948013 4      Public classMvcApplication:System.Web.HttpApplication5     {6          Public Static voidregisterglobalfilters (globalfiltercollection filters)7         {8Filters. ADD (NewHandleerrorattribute ());9         }Ten  One          Public Static voidregisterroutes (routecollection routes) A         { -Routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); -  the routes. MapRoute ( -                 "Default",//Route name -                 "{Controller}/{action}/{id}",//URL with Parameters -                 New{controller ="Home", action ="Index", id = urlparameter.optional}//Parameter Defaults +             ); -  +         } A  at         protected voidApplication_Start () -         { - Arearegistration.registerallareas (); -  - registerglobalfilters (globalfilters.filters); - registerroutes (routetable.routes); in         } -}

A Max_adminarearegistration.cs class that registers the route is also found in the area, with code snippets:

1 usingSYSTEM.WEB.MVC;2 3 namespaceMvcPro.Areas.Max_Admin4 {5      Public classmax_adminarearegistration:arearegistration6     {7          Public Override stringAreaName8         {9             GetTen             { One                 return "Max_admin"; A             } -         } -  the          Public Override voidRegisterarea (arearegistrationcontext context) -         { - context. MapRoute ( -                 "Max_admin_default", +                 "Max_admin/{controller}/{action}/{id}", -                 New{action ="Index", id =Urlparameter.optional} +             ); A         } at     } -}

The interface appears to have multiple Homecontoller, which is running out of the anomaly, which is also normal, through the reflection of finding HomeController find two, can not decide which one to execute, for granted.

How to deal with:

We modify the code in Global.asax.cs's Registerroute function separately:

1   Public Static voidregisterroutes (routecollection routes)2         {3Routes. Ignoreroute ("{Resource}.axd/{*pathinfo}");4 5 routes. MapRoute (6                 "Default",//Route name7                 "{Controller}/{action}/{id}",//URL with Parameters8                 New{controller ="Home", action ="Index", id = urlparameter.optional},//Parameter Defaults9                 New[] {"mvcpro.controllers" }Ten             ); One  A}

Modify the Max_adminarearegistration.cs function to modify the code in the Registerarea function:

1   Public Override voidRegisterarea (arearegistrationcontext context)2         {3 context. MapRoute (4                 "Max_admin_default",5                 "Max_admin/{controller}/{action}/{id}",6                 New{action ="Index", id = urlparameter.optional},//Parameter Defaults7                 New[] {"mvcPro.Areas.Max_Admin.Controllers" }8             );9}

Solution Reference: Http://stackoverflow.com/questions/7842293/multiple-types-were-found-that-match-the-controller-named-home

MVC Learning Series-controller

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.