Development Tool: vs2010 flagship Chinese edition
Error message:
Error Message
Multiple types that match the Controller named "home" are found. If no namespace is specified for the routing service provided by this request ("{controller}/{action}/{ID}") to search for controllers matching this request, this situation occurs. If so, register the route by calling the overload of the "maproute" method that uses the "namespaces" parameter.
The following matched controllers are found for the "home" request:
Mvcapplication1.areas. Web. controllers. homecontroller
Mvcapplication1.areas. admin. controllers. homecontroller
Steps for problem:
1. Create an empty ASP. net mvc 2 Project
2. Add two areas: Admin and website (one is the background and the other is the foreground)
3. Add the home controller to the two areas and create view files for the index method of each home controller respectively.
4. After completion, such:
Note that the MVC program that does not use area does not differentiate the namespace of cotroller, but the MVC program that uses area distinguishes the namespace of cotroller. the valid rule is "the Controller namespace must be under the namespace of the area". Suppose:
Adminarearegistration. the namespace in CS is mvcapplication1.areas. admin, the Controller namespace must be mvcapplication1.areas. admin. xxx. XXXX, that is, the namespace's leading part must be consistent; otherwise, an error is prompted.
In addition, in the Code of the global. asax. CS file
Code
Public static void registerroutes (routecollection routes)
{
Routes. ignoreroute ("{resource}. axd/{* pathinfo }");
Routes. maproute (
"Default", // route name
"{Controller}/{action}/{ID}", // URL with Parameters
New {controller = "home", Action = "Index", id = urlparameter. optional} // default value of the Parameter
);
}
Change controller = "home" to a controller name without duplicate names. Otherwise, the same error will be reported. Here we can change controller = "default ".
Is there any simpler and more flexible method? Of course, the most direct method is to modify the routing settings in adminarearegistration. CS and webarearegistration. CS, pass the namespace of the Controller to the system, and modify webarearegistration. CS as an example:
Code
Public override void registerarea (arearegistrationcontext context)
{
// Context. maproute (
// "Web_default ",
// "Web/{controller}/{action}/{ID }",
// New {controller = "home", Action = "Index", id = urlparameter. optional}
//);
// Pass the namespace directly
Context. maproute (
"Web_default ",
"Web/{controller}/{action}/{ID }",
New {controller = "home", Action = "Index", id = urlparameter. optional },
New String [] {"mvcapplication1.areas. Web. controllers "}
);
}
OK. It's over. I hope you can talk more!
Download the source code demo that can be run successfully:/files/taven/mvcapplication1.rar