新建一個空的Web項目 然後在Nuget庫中安裝下麵兩個包 Nancy Nancy.Hosting.Aspnet 然後在根目錄添加三個文件夾,分別是models,Module,Views 然後往Module文件夾裡面添加ConstraintRoutingModule類 然後往Models文件夾裡面添 ...
新建一個空的Web項目
然後在Nuget庫中安裝下麵兩個包
- Nancy
- Nancy.Hosting.Aspnet
然後在根目錄添加三個文件夾,分別是models,Module,Views
然後往Module文件夾裡面添加ConstraintRoutingModule類
public ConstraintRoutingModule() { Get("/",Lexan=>View["index"]); Get("/intConstraint/{value:int}",Lexan=>"這個值"+Lexan.value+ "是整數"); Get("/decimalConstraint/{value:decimal}",Lexan=>"這個值"+ Lexan.value+ "是十進位"); Get("/guidConstraint/{value:guid}",Lexan=>"這個值"+Lexan.value+ "是 guid"); Get("/boolConstraint/{value:bool}",Lexan=>"這個值"+Lexan.value+"是布爾值"); Get("/alphaConstraint/{value:alpha}", Lexan => "這個值" + Lexan.value + "是是信件"); Get("/datetimeConstraint/{value:datetime}", Lexan => "這個值" + Lexan.value + "是日期時間"); Get("/customDatetimeConstraint/{value:datetime(yyyy-MM-dd)}", Lexan => "這個值" + Lexan.value + "是一個日期時間, 格式為 'yyyy - mm - dd' "); Get("/minConstraint/{value:min(4)}", Lexan => "這個值" + Lexan.value + "是大於4的整數"); Get("/maxConstraint/{value:max(6)}", Lexan => "這個值" + Lexan.value + "是小於6的整數"); Get("/rangeConstraint/{value:range(10,20)}", Lexan => "這個值" + Lexan.value + "是介於10和20之間的整數"); Get("/minlengthConstraint/{value:minlength(4)}", Lexan => "這個值" + Lexan.value + "長度大於4的字元串"); Get("/maxlengthConstraint/{value:maxlength(10)}", Lexan => "這個值" + Lexan.value + "長度小於10的字元串"); Get("/lengthConstraint/{value:length(1,20)}", Lexan => "這個值" + Lexan.value + "是長度介於1和20之間的字元串"); Get("/versionConstraint/{value:version}", Lexan => "這個值" + Lexan.value + "是版本號"); Get("/emailConstraint/{value:email}", Lexan => "這個值" + Lexan.value + "是一個電子郵件地址"); }
然後往Models文件夾裡面添加EmailRouteSegmentConstraint類
public override string Name { get { return "email"; } } protected override bool TryMatch(string constraint, string segment, out string matchedValue) { //throw new NotImplementedException(); //使用 @jchannon 邏輯驗證電子郵件地址 if (segment.Contains("@")&&segment.Contains(".")) { matchedValue = segment; return true; } matchedValue = null; return false; }
然後往Views文件夾裡面添加index的html頁面
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> table { border-collapse:collapse; } table,th,td{ border:1px solid black; padding:2px 3px; } </style> </head> <body> <h1>約束路由</h1> <table>約束</table> <table> <thead> <tr> <th> 約束 </th> <th> 描述 </th> <th> 表達 </th> <th> 樣例 </th> </tr> </thead> <tbody> <tr> <td>alpha</td> <td>匹配大寫或小寫拉丁字母字元 (a-z、a-z)</td> <td>{x:alpha}</td> <td><a href="/alphaConstraint/foo">成功</a><a href="/alphaConstraint/1">失敗</a></td> </tr> <tr> <td>bool</td> <td>匹配布爾值</td> <td>{x:bool}</td> <td><a href="/boolConstraint/true">成功</a><a href="/boolConstraint/foo">失敗</a></td> </tr> <tr> <td>datetime</td> <td>匹配<strong>時間</strong>值</td> <td>{x:datetime}</td> <td><a href="/datetimeConstraint/1-1-2010">成功</a><a href="/datetimeConstraint/foo">失敗</a></td> </tr> <tr> <td>decimal</td> <td>匹配十進位值</td> <td>{x:decimal}</td> <td><a href="/decimalConstraint/1.5555555555555555555555555555">成功</a><a href="/decimalConstraint/foo">失敗</a></td> </tr> <tr> <td>guid</td> <td>匹配guid值</td> <td>{x:guid}</td> <td><a href="/guidConstraint/87e516f4-c759-430d-a552-18ad8e65483b">成功</a><a href="/guidConstraint/87e516f4-c759-430d-a552-18ad8e65483bd">失敗</a></td> </tr> <tr> <td>int</td> <td>匹配整數值</td> <td>{x:int}</td> <td><a href="/intConstraint/1">成功</a><a href="/intConstraint/foo">失敗</a></td> </tr> <tr> <td>length</td> <td>與指定長度或指定範圍內的字元串匹配</td> <td>{x:length(6)}<br />{x:length(1,20)}</td> <td><a href="/lengthConstraint/foobar">成功</a><a href="lengthConstraint/foobarfoobarfoobar">失敗</a></td> </tr> <tr> <td>max</td> <td>匹配具有最大值的整數</td> <td>{x:max(6)}</td> <td><a href="/maxConstraint/5">成功</a><a href="/maxConstraint/20">失敗</a></td> </tr> <tr> <td>maxlength</td> <td>匹配最大長度的字元串</td> <td>{x:maxlength(10)}</td> <td><a href="/maxlengthConstraint/foobar">成功</a><a href="/maxlengthConstraint/foobarfoobar">失敗</a></td> </tr> <tr> <td>min</td> <td>匹配具有最小值的整數</td> <td>{x:min(4)}</td> <td><a href="/minConstraint/5">成功</a><a href="/minConstraint/2">失敗</a></td> </tr> <tr> <td>minlength</td> <td>匹配最小長度的字元串</td> <td>{x:minlength(4)}</td> <td><a href="/minlengthConstraint/foobar">成功</a><a href="/minlengthConstraint">失敗</a></td> </tr> <tr> <td>range</td> <td>匹配值範圍內的整數</td> <td>{x:range(10,20)}</td> <td><a href="/rangeConstraint/15">成功</a><a href="/rangeConstraint/25">失敗</a></td> </tr> <tr> <td>version</td> <td>匹配版本號</td> <td>{x:version}</td> <td><a href="/versionConstraint/3.2.1">成功</a><a href="/versionConstraint/-4">失敗</a></td> </tr> <tr> <td>email</td> <td>匹配電子郵件地址</td> <td>{x:email}</td> <td><a href="/emailConstraint/[email protected]">成功過</a><a href="emailConstraint/maild@asdsacom">失敗</a></td> </tr> </tbody> </table> </body> </html>
然後修改Web.config配置文件
<httpHandlers> <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" /> </httpHandlers> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> <validation validateIntegratedModeConfiguration="false" /> <handlers> <add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" /> </handlers> </system.webServer>
然後按下F5運行
謝謝欣賞!明天同一時間更新!