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}
);
}
When debugging http://localhost:54500/ should route to http://localhost:54500/Home/Index.
Actually, the way you have it configured, http://localhost:54500/ will route to the HomeController.Index
method, not another URL.
The view 'Index' or its master was not found or no view engine supports the searched locations
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:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
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 the Root
route. This Root
route does not route to your Area. There are 2 ways to fix this.
Option 1 - Add the Root Route to the Home Area
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index" }
);
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
Option 2 - Set the DataToken to indicate the Home Area
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
).DataTokens["area"] = "Home";
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
In Core 1.0.1 you can do this in Startup.cs:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc(routes =>
{
routes.MapRoute(name: "areaRoute",
template: "{area:exists}/{controller=Home}/{action=Index}");
routes.MapRoute(
name: "default",
// template: "{controller=Home}/{action=Index}");
template: "{area=MyArea}/{controller=Home}/{action=Index}");
});
}