MVC supports routing with the same name, different namespaces

Source: Internet
Author: User

Sometimes we encounter two items together, then inevitably encounter two controllers of the same name, in fact, MVC in the registration of routes, add route when you can specify the current rule to resolve all controllers under that namespace.

Note: The controller's call is through Icontrollerfactory, reflection calls the target controller, does not specify the target namespace, directly from Buildmanager.getreferencedassemblies (); As follows.

  Private StaticList<type> controllertypes =NewList<type>(); /// <summary>        ///Static Constructors/// </summary>        Staticdefaultcontrollerfactory () {varAssemblys =buildmanager.getreferencedassemblies (); foreach(Assembly AssemblyinchAssemblys) {                varTypes = assembly. GetTypes (). Where (A =typeof(IController).                IsAssignableFrom (a)); foreach(Type typeinchtypes)                {Controllertypes.add (type); }            }        }        /// <summary>        ///Create a controller/// </summary>        /// <param name= "RequestContext" ></param>        /// <param name= "Controllername" ></param>        /// <returns></returns>         PublicIController Createcontroller (RequestContext RequestContext,stringcontrollername) {            stringTypeName = Controllername +"Controller"; Type Controllertype= Controllertypes.firstordefault (A = A.name = =)typeName); if(Controllertype! =NULL)            {                return(IController) activator.createinstance (Controllertype); }            return NULL; }

Back to the chase: How to support multiple namespaces

  Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{controller}/{action}", defaults:New{controller ="Home", action ="Index"}, namespaces:New string[] {"mvcexpose.controllers" }            ); Routes. MapRoute ("Admin","Admin/{controller}/{action}",                New{controller ="Home", action ="Index"},New string[] {"MvcExpose.Admin.Controllers" }); }

Remember: Two registration URL rules, to the length of inconsistent, default with "{Controller}/{action}/{id}" has been an error, that in the regular resolution, it is impossible to distinguish between the two routing rules, so when "http://localhost : 4500/admin/home/index ", the system first calls the default route resolution.

MVC supports routing with the same name, different namespaces

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.