Use of the open-source framework of NancyFx 2.0-ConstraintRouting and nancy framework

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.