亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

第一個匹配路由必須同時指定控制器和操作?

第一個匹配路由必須同時指定控制器和操作?

C#
叮當貓咪 2023-07-22 18:40:56
下面是我的代碼://inside UseMvc method:routes.MapRoute(   name: "NewRoute",   template: "/",   defaults: new { controller = "Home"});routes.MapRoute(   name: "default",    template: "{controller=Home}/{action=Index}/{id?}");我們知道路由系統只會找到第一個匹配的路由,所以第一個“NewRoute”應該在應用程序啟動時匹配路由,因為它沒有操作方法,所以我應該得到一個404錯誤頁面,但是當我運行應用程序時,使用“默認”路由,顯示正常頁面。那么為什么路由系統一開始就沒有選擇“NewRoute”呢?
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

事實是NewRoute先檢查,但路由找不到匹配的動作。然后它會匹配下一個路由規則。


Debug如果您啟用日志記錄級別appSettings.Development.json


{

"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).

匹配兩次并選擇default路線。


查看完整回答
反對 回復 2023-07-22
  • 1 回答
  • 0 關注
  • 191 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號