How can I tell my mvc
-application to route to a specific Controller
and Action
when they are not specified?
When debugging http://localhost:54500/
should route to http://localhost:54500/Home/Index
.
Currently I have:
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
But this always throws
The view 'Index' or its master was not found or no view engine supports the searched locations
Edit #1
It should redirect/route to an View which resides in a Area
called Home
. Just want to clarify, that there is a Controller
and an Area
which both are named Home
.
The config for the area is:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
Actually, the way you have it configured, http://localhost:54500/ will route to the
HomeController.Index
method, not another URL.This error indicates that routing succeeded, but the controller returned the path of a view that does not exist.
Since you also mentioned you are using an Area and have posted your configuration, it is clear what is happening. Your config is run in this order:
So, if you pass the URL
http://localhost:54500/
, the Area route will miss (because it doesn't start with/Home
) and it will match theRoot
route. ThisRoot
route does not route to your Area. There are 2 ways to fix this.Option 1 - Add the Root Route to the Home Area
Option 2 - Set the DataToken to indicate the Home Area
In Core 1.0.1 you can do this in Startup.cs: