<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ASP.NET Core端點(diǎn)路由中三種讓人困惑的路由函數(shù)

          共 1932字,需瀏覽 4分鐘

           ·

          2021-08-26 17:58


              早先提及了端點(diǎn)路由app.UseEndpoints, 端點(diǎn)路由強(qiáng)調(diào)的是端點(diǎn)路由,其核心目的是將請(qǐng)求落地點(diǎn)與路由尋址方式解耦。

          ASP.NET Core端點(diǎn)路由作用原理

          這里面有幾個(gè)容易混淆的函數(shù)

          ?MapControllerRoute?MapDefaultControllerRoute?MapControllers

          有什么不同?什么時(shí)候該用哪一個(gè)?


          1. MapControllerRoute

          Adds endpoints for controller actions to the Microsoft.AspNetCore.Routing.IEndpointRouteBuilder and specifies a route with the given name, pattern, defaults, constraints, and dataTokens.

          約定路由(conventional routing), 通常用在MVC項(xiàng)目中;

          需要傳參name  pattern defaults  constraints dataTokens;

          你可以在項(xiàng)目中這樣寫:

          endpoints.MapControllerRoute(
            name:"default",
            pattern:"{controller=Home}/{action=index}/{id?}"
          );

          如果請(qǐng)求url滿足 {host}{controller_name}{action_name}{option_id} , 將命中Controller=controller_name Action=action_name的方法體;如果url不提供controller、action名稱,默認(rèn)命中home/index 方法體。

          說到底這種寫法:
          是MVC web項(xiàng)目的早期寫法,讓用戶請(qǐng)求的url去匹配開發(fā)者的Controller-Action名稱。

          如今約定路由并不是主流,因?yàn)樗^的約定路由對(duì)于用戶瀏覽并不友好,而且暴露了后端開發(fā)者定義的瑣碎的Controller、Action名稱。

          實(shí)際上,不應(yīng)該讓用戶的url去匹配開發(fā)者定義的Controller-Action名稱(太丑陋的行為),而應(yīng)該讓開發(fā)者去匹配用戶想要使用的url, 這樣特性路由出現(xiàn)了

          2. MapDefaultControllerRoute

          Adds endpoints for controller actions to the Microsoft.AspNetCore.Routing.IEndpointRouteBuilder and adds the default route {controller=Home}/{action=Index}/{id?}.

          endpoints.MapDefaultControllerRoute(); 正是上面約定路由的默認(rèn)樣例,這沒什么好聊的。

          3. MapControllers

          Adds endpoints for controller actions to the Microsoft.AspNetCore.Routing.IEndpointRouteBuilder without specifying any routes.

          不對(duì)約定路由做任何假設(shè),也就是不使用約定路由,依賴用戶的特性路由, 一般用在WebAPI項(xiàng)目中。


          全文梳理就會(huì)發(fā)現(xiàn): 官方英文描述屢次出現(xiàn)的route,其實(shí)特指的是約定路由。

          這樣的描述我其實(shí)是不茍同的:  

          路由在.NET里面, 已經(jīng)被普世認(rèn)定為“約定路由”和“特性路由”,基于這種認(rèn)知,我讀了好幾遍官方英文描述,其實(shí)沒讀出個(gè)所以然的。

          官方英文描述使用 “route”來特指“約定路由”會(huì)誤導(dǎo)開發(fā)者。

          https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Core/src/Builder/ControllerEndpointRouteBuilderExtensions.cs

          本文內(nèi)容和制圖均為原創(chuàng),文章永久更新地址請(qǐng)參閱左下角原文,如對(duì)您有所幫助,請(qǐng)一鍵三連,方便的話置一個(gè)星標(biāo) ~。。~。

          瀏覽 58
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  操逼的三级片的视频 | 夜夜高潮夜夜爽精品视频 | 欧美99在线 | 夜夜爽妓女 | 韩日无码视频 |