Make sure you don't have a WebApiConfig.cs file in your App_Start folder. This may be overriding your route definition.
Controller cannot be found
-
07-07-2023 - |
Question
I'm working on an ASP.NET MVC 4 site. I have an ArtistController within the Controllers folder.
The ArtistController has a Get method:
public ActionResult Get(string paramOne, string paramTwo)
In RouteConfig.cs I have this route, before Default route:
routes.MapRoute(
name: "artistApi",
url: "api/artist/{action}/{id}",
defaults: new { controller = "Artist", action = "Get", id = UrlParameter.Optional }
);
When I try to test this call, I get:
No HTTP resource was found that matches the request URI. No type was found that matches the controller named 'Artist'.
It works if I remove the api part from the route:
url: "artist/{action}/{id}"
I am not using WebApi, just a normal Controller.
Any idea on why this route doesn't find the controller?
Thanks!
Solution
OTHER TIPS
Most likely your routes are not ordered properly - if api/
is before your more specific route it will match first and never reach your new route.
// More specific routes should go before generic ones:
routes.MapRoute(
name: "artistApi",
url: "api/artist/{action}/{id}",...);
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
If I understands correctly,
url: "api/artist/{action}/{id}" line takes id as your querystring
ex www.yourwebsite/youractioncontroller/1
your actionResult takes two querystring which MVC does not understand what to do
one morething is "artist" need to be your controller which normally {controller}
or your artist controller should be artistContoller