NancyFx 2.0的开源框架的使用-ConstraintRouting
新建一个空的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/mail@domain.com">成功过</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运行
谢谢欣赏!明天同一时间更新!
多看书,少装逼!