第一个匹配路由必须同时指定控制器和动作?
first match routes must specify both controller and action?
下面是我的代码:
//inside UseMvc method:
routes.MapRoute(
name: "NewRoute",
template: "/",
defaults: new { controller = "Home"});
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
我们知道路由系统只会找到第一个匹配的路由,所以第一个'NewRoute'应该在应用程序启动时匹配路由,因为它没有action方法,所以我应该得到一个404错误页面,但是当我 运行 应用程序时,使用了 "default" 路由,显示正常页面。那么为什么 "NewRoute" 没有首先被路由系统选中?
事实是先检查NewRoute
,但是路由找不到匹配的action.Then它将匹配下一个路由规则。
如果在 appSettings.Development.json
中将日志记录级别设为 Debug
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Debug"
}
}
}
并将启动的CompatibilityVersion改为2.1(asp.net core 2.2有另一种EndPoint机制)
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
当你运行应用程序时,你可以在日志中看到整个过程:
dbug: Microsoft.AspNetCore.Routing.RouteBase[1]
Request successfully matched the route with name 'NewRoute' and template '/'
dbug: Microsoft.AspNetCore.Mvc.Internal.ActionSelector[3]
No actions matched the current request. Route values: controller=Home
dbug: Microsoft.AspNetCore.Mvc.Internal.MvcRouteHandler[3]
No actions matched the current request. Route values: controller=Home
dbug: Microsoft.AspNetCore.Routing.RouteBase[1]
Request successfully matched the route with name 'default' and template '{controller=Home}/{action=Index}/{id?}'
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3]
Route matched with {action = "Index", controller = "Home"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Index() on controller Core22MVC.Controllers.HomeController (Core22MVC).
它匹配两次 select default
路由。
下面是我的代码:
//inside UseMvc method:
routes.MapRoute(
name: "NewRoute",
template: "/",
defaults: new { controller = "Home"});
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
我们知道路由系统只会找到第一个匹配的路由,所以第一个'NewRoute'应该在应用程序启动时匹配路由,因为它没有action方法,所以我应该得到一个404错误页面,但是当我 运行 应用程序时,使用了 "default" 路由,显示正常页面。那么为什么 "NewRoute" 没有首先被路由系统选中?
事实是先检查NewRoute
,但是路由找不到匹配的action.Then它将匹配下一个路由规则。
如果在 appSettings.Development.json
Debug
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Debug"
}
}
}
并将启动的CompatibilityVersion改为2.1(asp.net core 2.2有另一种EndPoint机制)
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
当你运行应用程序时,你可以在日志中看到整个过程:
dbug: Microsoft.AspNetCore.Routing.RouteBase[1]
Request successfully matched the route with name 'NewRoute' and template '/'
dbug: Microsoft.AspNetCore.Mvc.Internal.ActionSelector[3]
No actions matched the current request. Route values: controller=Home
dbug: Microsoft.AspNetCore.Mvc.Internal.MvcRouteHandler[3]
No actions matched the current request. Route values: controller=Home
dbug: Microsoft.AspNetCore.Routing.RouteBase[1]
Request successfully matched the route with name 'default' and template '{controller=Home}/{action=Index}/{id?}'
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3]
Route matched with {action = "Index", controller = "Home"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Index() on controller Core22MVC.Controllers.HomeController (Core22MVC).
它匹配两次 select default
路由。