Use of the open-source framework of NancyFx 2.0-ConstraintRouting and nancy framework
Create an empty Web Project
Then install the following two packages in the Nuget Library:
- Nancy
- Nancy. Hosting. Aspnet
Then add three folders in the root directory: models, Module, and Views.
Add the ConstraintRoutingModule class to the Module folder.
Public ConstraintRoutingModule () {Get ("/", Lexan => View ["index"]); Get ("/intConstraint/{value: int }", lexan => "this value" + Lexan. value + "is an integer"); Get ("/decimalConstraint/{value: decimal}", Lexan => "this value" + Lexan. value + "in decimal format"); Get ("/guidConstraint/{value: guid}", Lexan => "this value" + Lexan. value + "is guid"); Get ("/boolConstraint/{value: bool}", Lexan => "this value" + Lexan. value + "is a Boolean value"); Get ("/alphaConstraint/{value: alpha}", Lexan => "this value" + Lexan. value + "is a letter"); Get ("/datetimeConstraint/{value: datetime}", Lexan => "this value" + Lexan. value + "Date and Time"); Get ("/customDatetimeConstraint/{value: datetime (yyyy-MM-dd)}", Lexan => "this value" + Lexan. value + "is a date and time in the format of 'yyyy-mm-dd'"); Get ("/minConstraint/{value: min (4 )}", lexan => "this value" + Lexan. value + "is an integer greater than 4"); Get ("/maxConstraint/{value: max (6)}", Lexan => "this value" + Lexan. value + "an integer smaller than 6"); Get ("/rangeConstraint/{value: range ()}", Lexan => "this value" + Lexan. value + "is an integer between 10 and 20"); Get ("/minlengthConstraint/{value: minlength (4)}", Lexan => "this value" + Lexan. value + "string greater than 4"); Get ("/maxlengthConstraint/{value: maxlength (10)}", Lexan => "this value" + Lexan. value + "string less than 10"); Get ("/lengthConstraint/{value: length ()}", Lexan => "this value" + Lexan. value + "is a string between 1 and 20"); Get ("/versionConstraint/{value: version}", Lexan => "this value" + Lexan. value + "version"); Get ("/emailConstraint/{value: email}", Lexan => "this value" + Lexan. value + "is an email address ");}
Add the EmailRouteSegmentConstraint class to the Models folder.
Public override string Name {get {return "email" ;}} protected override bool TryMatch (string constraint, string segment, out string matchedValue) {// throw new NotImplementedException (); // use @ jchannon to logically verify the email address if (segment. contains ("@") & segment. contains (". ") {matchedValue = segment; return true;} matchedValue = null; return false ;}
Add the index html page to the Views folder.
<! DOCTYPE html>
Then modify the Web. config configuration file
Then press F5 to run
Thank you! Update at the same time tomorrow!