应用程序根的默认路由
Default-Route for root of application
如何让我的 mvc
应用程序路由到未指定的特定 Controller
和 Action
?
调试时 http://localhost:54500/
应该路由到 http://localhost:54500/Home/Index
。
目前我有:
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
但这总是抛出
The view 'Index' or its master was not found or no view engine supports the searched locations
编辑#1
它应该 redirect/route 到驻留在名为 Home
的 Area
中的视图。只是想澄清一下,有一个 Controller
和一个 Area
都被命名为 Home
.
该区域的配置是:
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.
实际上,按照您的配置方式,http://localhost:54500/ 将路由到 HomeController.Index
方法,而不是另一个 URL.
The view 'Index' or its master was not found or no view engine supports the searched locations
这个错误表示路由成功,但是controller返回了一个不存在的view的路径。
既然你也提到你正在使用一个区域并且已经发布了你的配置,那么很清楚发生了什么。您的配置是 运行 的顺序:
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 }
);
因此,如果您通过 URL http://localhost:54500/
,Area 路线将会错过(因为它不是以 /Home
开头)并且它将匹配 Root
路线。此 Root
路线未通往您所在的地区。有两种方法可以解决此问题。
选项 1 - 将根路由添加到主区域
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 }
);
选项 2 - 设置 DataToken 以指示主区域
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 }
);
在 Core 1.0.1 中,您可以在 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}");
});
}
如何让我的 mvc
应用程序路由到未指定的特定 Controller
和 Action
?
调试时 http://localhost:54500/
应该路由到 http://localhost:54500/Home/Index
。
目前我有:
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
但这总是抛出
The view 'Index' or its master was not found or no view engine supports the searched locations
编辑#1
它应该 redirect/route 到驻留在名为 Home
的 Area
中的视图。只是想澄清一下,有一个 Controller
和一个 Area
都被命名为 Home
.
该区域的配置是:
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.
实际上,按照您的配置方式,http://localhost:54500/ 将路由到 HomeController.Index
方法,而不是另一个 URL.
The view 'Index' or its master was not found or no view engine supports the searched locations
这个错误表示路由成功,但是controller返回了一个不存在的view的路径。
既然你也提到你正在使用一个区域并且已经发布了你的配置,那么很清楚发生了什么。您的配置是 运行 的顺序:
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 }
);
因此,如果您通过 URL http://localhost:54500/
,Area 路线将会错过(因为它不是以 /Home
开头)并且它将匹配 Root
路线。此 Root
路线未通往您所在的地区。有两种方法可以解决此问题。
选项 1 - 将根路由添加到主区域
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 }
);
选项 2 - 设置 DataToken 以指示主区域
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 }
);
在 Core 1.0.1 中,您可以在 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}");
});
}