Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Resolve Ambiguous Controller Error by routes

Resolve Ambiguous Controller Error by routes

03 Jul 2024
Intermediate
151K Views
2 min read
Learn with an interactive course and practical hands-on labs

ASP.NET MVC with Web API Foundations Course - Free

In previous articles, I have described the Routing System and how to create Route Constraints in your application. Now the time is to resolve the common error "multiple matching controllers were found" raised by the routing system when your application have more than one controller with the same name in different namespace.

Raised Error

Suppose you have HomeController with in two namespaces : Mvc4_RouteConstraints & Mvc4_Route. You have also registered a Default route for your application as shown below.

routes.MapRoute(
 "Default", // Route name
 "{controller}/{action}/{id}", // Route Pattern
 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Default values for parameters
);

So when you will run your application then it will throw the error that the request for 'Home' has found the more than one matching controllers as shown below fig.

How to resolve it...

We can resolve this error by setting priority of the controllers. Suppose in the application HomeCOntroller defined in the Mvc4_RouteConstraints namespace has high priority than Mvc4_Route namespace controller. Let's do it.

routes.MapRoute(
 "Default", // Route name
 "{controller}/{action}/{id}", // Route Pattern
 new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Default values for parameters
 new[] { "Mvc4_RouteConstraints"});
);

In above code, I have added the Mvc4_RouteConstraints namespace as a string which told the MVC framework to look in the Mvc4_RouteConstraints namespace before looking anywhere else. Now, when you run your application, it will run successfully without any error.

If you want to give preference to a controller with in one namespace and all other controllers should also resolved in another namespace, you need to defined multiple routes as shown below.

routes.MapRoute(
 "Default", // Route name
 "{controller}/{action}/{id}", // Route Pattern
 new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Default values for parameters
 new[] { "Mvc4_RouteConstraints"});
);

routes.MapRoute(
 "Default", // Route name
 "{controller}/{action}/{id}", // Route Pattern
 new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Default values for parameters
 new[] { "Mvc4_Route"});
);
What do you think?

I hope you have got how to resolve ambiguous controller error by defining routes. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this