Asp.net 应用程序根目录的默认路由
我如何告诉我的Asp.net 应用程序根目录的默认路由,asp.net,asp.net-mvc,routes,url-routing,asp.net-mvc-routing,Asp.net,Asp.net Mvc,Routes,Url Routing,Asp.net Mvc Routing,我如何告诉我的mvc-应用程序在未指定的情况下路由到特定的控制器和操作 调试http://localhost:54500/应发送至http://localhost:54500/Home/Index 目前我有: routes.MapRoute( name: "Root", url: "", defaults: new { controller = "Home", action = "Index" } ); routes.MapRoute( name: "D
mvc
-应用程序在未指定的情况下路由到特定的控制器
和操作
调试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 }
);
但这总是让人失望
未找到视图“索引”或其主视图,或者没有视图引擎支持搜索的位置
编辑#1
它应该重定向/路由到位于名为Home
的区域中的视图。只想澄清一下,有一个控制器
和一个区域
,它们都被命名为主页
该区域的配置为:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
调试何时应路由到
实际上,您配置它的方式将路由到HomeController.Index
方法,而不是另一个URL
未找到视图“索引”或其主视图,或者没有视图引擎支持搜索的位置
此错误表示路由已成功,但控制器返回的视图路径不存在
因为您还提到您正在使用一个区域,并且已经发布了您的配置,所以很清楚发生了什么。您的配置按以下顺序运行:
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 }
);
因此,如果您传递URLhttp://localhost:54500/
,区域路由将丢失(因为它不是以/Home
开头),并且它将与根路由匹配。此根路径
路径不会路由到您所在的区域。有两种方法可以解决这个问题
选项1-将根路由添加到主区域
选项2-设置DataToken以指示主区域
在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}");
});
}
在项目的视图/主文件夹下是否有名为Index.cshtml的视图?调用http://localhost:54500/Home/Home/Index
工作正常。这意味着您的文件夹层次结构错误。它应该是Views/Home/Index.cshtml
可能是Area
s的问题,但除非您发布整个路由配置(按顺序),否则无法判断哪个路由首先匹配。路由的区域配置在哪里?您提到您正在使用区域。您也在使用属性路由吗?要解决这个问题,需要考虑更多的路由配置。第一场比赛获胜,而不是最好的比赛。因此,要先查看匹配项,我们需要查看整个配置。请查看我更新的问题。而且,不,目前没有属性运算
设置选择选项#2
,因为这更符合我的需要,而且在以后添加越来越多的区域
时感觉像是一个很好的解决方案,而不考虑默认的(-root)路径。谢谢
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 }
);
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}");
});
}