I have an MVC4 project with language selection:
1 main part with:
This could be because the path was wrong. So check the path and the spelling of the controller first. In my case, my controller was named CampsController, and the WebApiConfig.cs file had an extra path in it.
Instead of: http://localhost:6600/Camps
It was: http://localhost:6600/api/Camps
I had not noticed the api word in the WebApiConfig.cs file:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Also it could be because the controller was incorrectly named. Here I called LayoutController, but should have called Layout instead:
<a class="nav-link" href="#">@Html.Action("GetCurrentUser", "LayoutController" })</a>
should be:
<a class="nav-link" href="#">@Html.Action("GetCurrentUser", "Layout")</a>
Another example, it could be because you have bad Route paths defined. Make sure your paths are correct. Example:
[RoutePrefix("api/camps")]
public class CampsController : ApiController
[Route("{moniker}")]
public async Task<IHttpActionResult> Get(string moniker)
I've found it.
When a page, that is located inside an area, wants to access a controller that is located outside of this area (such as a shared layout page or a certain page inside a different area), the area of this controller needs to be added. Since the common controller is not in a specific area but part of the main project, you have to leave area empty:
@Html.Action("MenuItems", "Common", new {area="" })
The above needs to be added to all of the actions and actionlinks since the layout page is shared throughout the various areas.
It's exactly the same problem as here: ASP.NET MVC Areas with shared layout
Edit: To be clear, this is marked as the answer because it was the answer for my problem. The above answers might solve the causes that trigger the same error.
This error can also be caused by the fact that Controllers must have (in their name) the word Controller
; viz: HomeController
; unless you implement your own ControllerFactory
.
Here is my problem and the solution that what worked for me.
I added a new controller with a single action returning a string to an existing application. But when I navigated to that controller via browser, I was getting the same error as mentioned above.
After doing lot of googling, I found out that I simply had to modify my Global.asax.cs file for it to recognize the new controller. All I did was added a space to Global.asax.cs file so that it is modified and it worked
Yet another possible root cause for this error is if the namespace for the area registration class does not match the namespace for the controller.
E.g. correct naming on controller class:
namespace MySystem.Areas.Customers
{
public class CustomersController : Controller
{
...
}
}
With incorrect naming on area registration class:
namespace MySystem.Areas.Shop
{
public class CustomersAreaRegistration : AreaRegistration
{
...
}
}
(Namespace above should be MySystem.Areas.Customers
.)
Will I ever learn to stop copy and pasting code? Probably not.
In my case in global.asax/application_start method, I was registering web api routes AFTER mvc routes like so:
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(WebApiConfig.Register);
Reverting the order fixed the issue
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);