Asp.net MVC 2 common errors when using the areas function

Source: Internet
Author: User

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

 

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.