I am running into the following error with my ASP.NET MVC 3 project:
Multiple types were found that match
the controller named 'Home'. This can
happen if the route that services this
request ('Home/{action}/{id}') does
not specify namespaces to search for a
controller that matches the request.
If this is the case, register this
route by calling an overload of the
'MapRoute' method that takes a
'namespaces' parameter.
The request for 'Home' has found the
following matching controllers:
MyCompany.MyProject.WebMvc.Controllers.HomeController
MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController
I have a HomeController in my default controller folder, with a class name of MyCompany.MyProject.WebMvc.Controllers.HomeController.
My RegisterRoutes method, in my global.asax, looks like:
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 } // Parameter defaults
);
}
I then have an area called Company, with a HomeController in the default controller folder for the area, with a class name of MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController.
The RegisterArea method in the CompanyAreaRegistration file looks like:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Company_default",
"Company/{controller}/{action}/{id}",
new { area = "Company", action = "Index", id = UrlParameter.Optional }
);
}
This is all leading the error I highlighted at the beginning of this post. I am struggling trying to piece together a solution from various other posts, with NO LUCK.
Is it possible to have a HomeController in the default controllers folder and then one in EACH area? If so, do I need to make (assuming I do) changes to my configuration file to make this work?
Any help would be much appreciated!
The error message contains the recommended solution: "If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter."
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "MyCompany.MyProject.WebMvc.Controllers"}
);
This will make http://server/ go to your HomeController's Index action which is, I think, what you want. http://server/company/home will go to the Company area's HomeController's Index action, as defined in the area registration.
This is the asp.net mvc4 approach:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "RegisterNow", id = UrlParameter.Optional },
namespaces: new[] { "YourCompany.Controllers" }
);
I had renamed the namespaces, so, i only delete de folders bin and obj and rebuild, work again.
If you're using RazorGenerator, just informing the namespaces parameter could be not enough.
I got to solve adding the statement marked below at Global.asax.cs:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ControllerBuilder.Current.DefaultNamespaces.Add("MyProject.Controllers"); // This one
}
Another plausible cause of this issue could be found below:
Multiple types were found that match the controller named 'Home'
use this
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "ProjectName.Controllers" }
);
Use only the name of the project:
Public Class RouteConfig
Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.MapRoute( _
name:="Default", _
url:="{controller}/{action}/{id}", _
defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
, namespaces:={"MvcAreas"})
End Sub
I had the same issue and found that the older version had created compiled files in the "bin" folder.
Once I deleted these the error disappeared.
As Chris Moschini mention the namespaces parameter may not be enough if you have two areas with same controller name with different namespaces and the default none area route will return 500 server error.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "MyCompany.MyProject.WebMvc.Controllers"}
);
It "best" to override the default route handler and add this line:
RequestContext.RouteData.DataTokens["UseNamespaceFallback"] = false;
I had this issue after I added a reference to another project that had the same routes and the problem continued after I removed the reference.
Resolved by deleting the .dll file of that added reference from the bin folder and rebuilding.
Like many others, I had this problem after creating a new MVC template project from VS2017 menu, on building the project I would get the op's error message. I then used the answer https://stackoverflow.com/a/15651619/2417292 posted earlier in this thread by cooloverride
for mvc4 projects.
This still didn't fix my issue so I renamed my Home view folder and HomeController file to be the view folder Company/ and controller file CompanyController. This then worked for me, not a fix per say but a workaround if your not stuck on having the route Home/Index my other issue was I couldn't find the reference causing the error and due to my dev platform being Azure WebApplication vs a full VM with a complete file system and IIS settings to mess with.
For MVC5 below code works for issue same controller name for different areas. You have to specify which area controller have to hit first.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "ApplicationName.Areas.AreaName.Controllers" }
).DataTokens.Add("area", "AreaName");
This issue occurred when I accidentally added
[Route("api/[controllers]s")]
instead of
[RoutePrefix("api/[controllers]s")]
to the top of my ApiController.
Related
I have problem with routing in my mvc app. I have my core app with ProductsController and extension that is loaded in "PreApplicationStartMethod". In this extenstion i register route like this:
routes.MapRoute(
name: "ProductDetails",
url: "Products/Details/{id}",
defaults: new { controller = "StandardProductsDetails", action = "Details" },
namespaces: new []{ "Shop.Controllers" }
);
And in my core app im not registering specific route for ProductsController but I have a general route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Shop.Controllers" }
);
Now when I enter /Products/Details/1 in web browser im redirectd to ProductsController.
Oderd of adding routes is specific first, general last. I've checked routes table and my route is existing in it.
I think you are using two projects here, if you are using then make the project as startup project(by right click) which you want to hit first.
I have added WCF service to MVC 5 application, and created a route for it:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new ServiceRoute("Service1.svc", new ServiceHostFactory(), typeof(Service1)));
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
The problem is that all my links leads to the Service1.svc route now. #Html.ActionLink("Passport Maker", "Index", "Home", new { area = "" }, new { #class = "navbar-brand" }) become http://localhost:50099/Service1.svc?action=Index&controller=Home and other links change in the same way.
If I add ServiceRoute after "Default" route, the links work correctly but the service is unavailable.
Why it happens (there is no "Service1" in the links, why they select the service route then?) and how to fix it?
The solution:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { controller = "^(?!Service1.svc).*" }
);
routes.Add(new ServiceRoute("Service1.svc", new ServiceHostFactory(), typeof(Service1)));
Explanations for those who may encounter a similar problem: the reason of the problem was that Html.ActionLink uses the first matching route to generate the link. And my service route was the first and was matching, because a route does not require to include {controller} and {action} parameters to be matched (as I thought initially).
The solution is to put the default route first, so it is used by Html.ActionLink. And to still be able to use the service route, need to exclude it from the the first route using constraints. The regex ^(?!Service1.svc).* matches only those controller names that don't start from "Service1.svc".
I have the problem where URL's are being added on the current URL for example:
Localhost/Config -> Localhost/Config/Profile
By making the call #Html.ActionLink(Resources.General.Achievements, "Index", "Profile")
When what I really want is: Localhost/Profile
I know this is possible using MVC Routes, I just can't figure out how to call them from the View. I have used Html.BeginRouteForm, but this route won't be triggered on submit.
This is my route config
routes.MapRoute(
name: "Profile",
url: "Profile/{id}",
defaults: new { controller = "Profile", action = "Index" },
namespaces: new[] { "Cobalt.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Cobalt.Controllers" }
);
I am trying to access the Profile controller and Index action in the root directory. I have both Config and Manage Areas, which I am trying to escape from when making calls back to controllers in the root directory.
Thanks in advance for any help
When dealing with areas, if you want to move from one area to another area or out of an area into the root, you can use the overload of ActionLink which allows you to specify route data and pass an empty string (or the appropriate area name) into area:
#Html.ActionLink(Resources.General.Achievements, "Index", "Profile", new { area = string.Empty })
Use RouteLink:
#Html.RouteLink(Resources.General.Achievements, "Profile", new { id = "1234" })
Do note that since your "id" parameter is not declared optional on your Profile route it won't match unless you supply it.
I'm currently working at a .NET 4.5 MVC 4 Web Application.
I have got the following Routes:
routes.MapRoute(
name: "Default",
url: "api/",
defaults: new { controller = "Response", action = "ReturnAllStations" }
);
routes.MapRoute(
name: "ID",
url: "api/{id}",
defaults: new { controller = "Response", action = "ReturnStuffA", id = UrlParameter.Optional }
);
Now when I enter the URL http://localhost:55302/api/ it all works fine. But when I enter an URL like this: http://localhost:55302/api/SampleId1234 I get the following error "No type was found that matches the controller named 'Sample1234'."
Why does it try to get a controller named Sample1234 and not the defautlt one and use sample1234 as parameter?
Your default route should come last. Route config will look for the configuration from top to bottom and when it finds a match immediately returns invokes that action.
In your case always invoke the first configuration because it matches the api/ configuration.
Could someone show me how to use the MapRoute method? I have tried creating my own routes, but it's not working. What i want to accomplish is a route that routes "http://servername/home/default.aspx" into controller "Home" and action "Default". Also, would it be possible to say that if the user is browsing the default.aspx "file", it would actually point to the "Index" action?
I have tried reading the MSDN references and googling, but it didn't make me any wiser.
Probably too late to help the developer who raised the question but may help someone else. New to MVC but what I found is the map routes seem to be processed in the order they are added. I had a similar problem, my specific route was not working until I started adding the default route as the last route.
If the default map route is added before your custom one and your custom URL matches the structure defined by the default map route you will never reach your custom route.
The route you want to configure the first part of your question is:
routes.MapRoute(
"",
"home/default.aspx",
new { controller = "Home", action = "Default" }
);
Assuming you wish to 'browse' default.aspx with some sort of parameter you can do something like:
routes.MapRoute(
"",
"home/default.aspx/{param}",
new { controller = "Home", action = "Default", param = UrlParameter.Optional }
);
And you would then need to create your Default action to accept string param.
You also have to make sure the parameter name is the same as the action's parameter name.
Example:
routes.MapRoute(
name: "MyName",
url: "{controller}/{action}/{myParam}",
defaults: new { controller = "MyController", action = "MyAction", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
MyController:
public ActionResult MyAction(string myParam = "")
{
}