0%

【MVC教學】4. 為你的Route加一點限制

上一篇寫了Route比對的邏輯,這次來點更進階的應用,讓我們幫Route比對加上一些些限制。

假設今天About的頁面,他是依據網址帶入的 ID取得對應的會員資料,回傳結果,而ID必定為數字,如果不是數字就不要進到程式碼,直接擋掉該如何做?

1
2
3
//我們希望的網址,最後的ID一定要為數字
/home/about/1

加上Constraints限制,而限制的方法用正規表示法來表達,以上述的只能為數字為例

[![](https://1.bp.blogspot.com/-DDzFIwOxsvQ/WsTtsAb1bgI/AAAAAAAAInA/pJdFwqPGRdIZ6LKQ-EgiXc2xem8FcQ4QACLcBGAs/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.22.03.png)](https://1.bp.blogspot.com/-DDzFIwOxsvQ/WsTtsAb1bgI/AAAAAAAAInA/pJdFwqPGRdIZ6LKQ-EgiXc2xem8FcQ4QACLcBGAs/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.22.03.png)
constraints的部分,我們把ID限制在只能出現數字,(如果對於正規表示法不熟悉的話,推薦可以翻翻這本書,就算記不起來拿來當工具書也很實用 [處理大數據的必備美工刀 - 全支援中文的正規表示法精解](https://www.tenlong.com.tw/products/9789863759539))

接著執行網站試試看 /home/about/123

[![](https://2.bp.blogspot.com/-5v8kKEHK7oQ/WsTve5DgK7I/AAAAAAAAInM/qCpoatJcmjED_wL1aP5zPUEnHuiVDzA1QCLcBGAs/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.29.45.png)](https://2.bp.blogspot.com/-5v8kKEHK7oQ/WsTve5DgK7I/AAAAAAAAInM/qCpoatJcmjED_wL1aP5zPUEnHuiVDzA1QCLcBGAs/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.29.45.png)
看起來沒問題,接著我們執行/home/about/Steven,這邊請記得把Default那組Route註解起來,如果還記得上篇Route比對方法的話,這組雖然會因為Steven不是數字而被About那組Route擋掉,但依然符合Default的萬用Route比對規則,而正確執行,為了測試請先註解掉Default那組。
[![](https://3.bp.blogspot.com/-97qSMhzIPOE/WsTwIiBOlMI/AAAAAAAAInU/Xb90Cy9gzJYsGtOuzXkPSL6bWfQszpr1ACLcBGAs/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.32.39.png)](https://3.bp.blogspot.com/-97qSMhzIPOE/WsTwIiBOlMI/AAAAAAAAInU/Xb90Cy9gzJYsGtOuzXkPSL6bWfQszpr1ACLcBGAs/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.32.39.png)

如我們預期的,因為Steven不是數字的原因而被擋掉了,Constraints算是一個可以把Route用得更靈活的技巧,雖然需要懂的正規表示法,但我覺得這兩項學習投資很划算,正規表示法到很多地方都很萬用。

自訂更複雜的Constraints

我們再來出個更刁的要求,假設你老闆就叫Steven,而且他不希望跟別人一樣,每個人都是打ID查資料顯示太一般,他偏偏要只有輸入Steven也要能進到About頁時該怎麼辦?
當然也可以用正規表示法硬做,但可能會讓Constraint寫得很醜難維護,所以這次改用實作IRouteConstraint的方式來完成這個需求
建立一個StevenBossConstraint的Class
[![](https://1.bp.blogspot.com/-lzkbxUNTjPs/WsTyHmO8DrI/AAAAAAAAInk/Z69_UKTgzw4Yiduel0QPOKVnIq9uNLP1ACLcBGAs/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.39.46.png)](https://1.bp.blogspot.com/-lzkbxUNTjPs/WsTyHmO8DrI/AAAAAAAAInk/Z69_UKTgzw4Yiduel0QPOKVnIq9uNLP1ACLcBGAs/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.39.46.png)
實作以下內容
[![](https://3.bp.blogspot.com/-mCMOFMxoiQs/WsT0UDLnh5I/AAAAAAAAIn0/w7PzqK5RsyEabGIx9azbwzT2CZj6VlN6gCLcBGAs/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.48.47.png)](https://3.bp.blogspot.com/-mCMOFMxoiQs/WsT0UDLnh5I/AAAAAAAAIn0/w7PzqK5RsyEabGIx9azbwzT2CZj6VlN6gCLcBGAs/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.48.47.png)

將原本設定ID的Constrainte改成我們寫的StevenBossConstraint

[![](https://3.bp.blogspot.com/-SXNFt3bhpRE/WsT1HLb4yzI/AAAAAAAAIn8/x04sB1B-ymwBSfdvYbCOrn4EcNjTaoFAwCLcBGAs/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.51.38.png)](https://3.bp.blogspot.com/-SXNFt3bhpRE/WsT1HLb4yzI/AAAAAAAAIn8/x04sB1B-ymwBSfdvYbCOrn4EcNjTaoFAwCLcBGAs/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.51.38.png)

執行後就會發現,數字跟Steven都可以通過Route的檢查,但你打Tom或是Tim之類的其他非數字參數,都會被擋掉

[![](https://2.bp.blogspot.com/-QOP-fMjmh1M/WsT1zhvaGSI/AAAAAAAAIoM/8GxzEl2aKKY0V2sSKZEo9zXqNg2qI7KJQCLcBGAs/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.56.40.png)](https://2.bp.blogspot.com/-QOP-fMjmh1M/WsT1zhvaGSI/AAAAAAAAIoM/8GxzEl2aKKY0V2sSKZEo9zXqNg2qI7KJQCLcBGAs/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.56.40.png)
[![](https://2.bp.blogspot.com/-dV8vMrVM9rQ/WsT1z2-D7uI/AAAAAAAAIoQ/TJAfdgiSc_cu36opnnSktdBx9eULS0tXwCLcBGAs/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.56.29.png)](https://2.bp.blogspot.com/-dV8vMrVM9rQ/WsT1z2-D7uI/AAAAAAAAIoQ/TJAfdgiSc_cu36opnnSktdBx9eULS0tXwCLcBGAs/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.56.29.png)
[![](https://3.bp.blogspot.com/-rAGzQD3L_c8/WsT1zmOtcFI/AAAAAAAAIoI/O7utdb1IfIUkMAK013X5TQwoo-QOvQ-pACLcBGAs/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.56.47.png)](https://3.bp.blogspot.com/-rAGzQD3L_c8/WsT1zmOtcFI/AAAAAAAAIoI/O7utdb1IfIUkMAK013X5TQwoo-QOvQ-pACLcBGAs/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-04%2B%25E4%25B8%258B%25E5%258D%258811.56.47.png)

透過小工具來幫助偵錯Route設定

可以透過Nuget來安裝Route Debugger工具,他們告訴我們目前網頁之所以能夠顯示,是因為走了哪一條Route規則,這在初期我對這些設定還不熟悉時幫助非常的大
[![](https://1.bp.blogspot.com/-OF3Lz23taZc/WsT2n5WMicI/AAAAAAAAIoc/yvgdxPJ_Dj84sOOWiZaf5KDe4m47nuFOQCLcBGAs/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-05%2B%25E4%25B8%258A%25E5%258D%258812.00.15.png)](https://1.bp.blogspot.com/-OF3Lz23taZc/WsT2n5WMicI/AAAAAAAAIoc/yvgdxPJ_Dj84sOOWiZaf5KDe4m47nuFOQCLcBGAs/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-05%2B%25E4%25B8%258A%25E5%258D%258812.00.15.png)
安裝這組套件
[![](https://2.bp.blogspot.com/-MY03_784a74/WsT5eh-6T7I/AAAAAAAAIoo/fgvoj60WvfARLWeyjMfRHi_9onyYPdCmACLcBGAs/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-05%2B%25E4%25B8%258A%25E5%258D%258812.10.10.png)](https://2.bp.blogspot.com/-MY03_784a74/WsT5eh-6T7I/AAAAAAAAIoo/fgvoj60WvfARLWeyjMfRHi_9onyYPdCmACLcBGAs/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-05%2B%25E4%25B8%258A%25E5%258D%258812.10.10.png)

接著重新執行網站就可在看到詳細解說

[![](https://1.bp.blogspot.com/-_GBLWNdFSoA/WsT51ZoPPkI/AAAAAAAAIos/A4wI4Ml7U9U1QjmMwGcIPe0LQCpVbra2QCLcBGAs/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-05%2B%25E4%25B8%258A%25E5%258D%258812.13.24.png)](https://1.bp.blogspot.com/-_GBLWNdFSoA/WsT51ZoPPkI/AAAAAAAAIos/A4wI4Ml7U9U1QjmMwGcIPe0LQCpVbra2QCLcBGAs/s1600/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7%2B2018-04-05%2B%25E4%25B8%258A%25E5%258D%258812.13.24.png)

相信剛開始要改Route的使用者來說,會是相當有幫助的工具喔!!