Nancy combined with tinyfox provides simple data services for our applications.
I. prerequisite work
Create a new empty web application
Add appropriate assemblies, configure TinyFox for local debugging, etc.
For more information, see Nancy's Nancy.Owin-based mini Demo.
Second, write our Startup.cs
1 using Microsoft.Owin; 2 using Owin; 3 [assembly: OwinStartup (typeof (NancyAPIDemo.Startup))] 4 namespace NancyAPIDemo 5 {6 public class Startup 7 {8 public void Configuration (IAppBuilder app) 9 {10 app.UseNancy (); 11} 12} 13}
Third, write our Module
Just give me a name, as follows
1 using Nancy; 2 namespace NancyAPIDemo 3 {4 public class HomeModule: NancyModule 5 {6 private static User user = new User {UName = "catcher", UGender = "m"}; 7 public HomeModule () 8 {9 Get ["/ getdata"] = _ = > 10 {11 return Response.AsJson (user); 12} 13} 14} 15 public class User16 {17 public string UName {get; set;} 18 public string UGender {get; set;} 19} 20}
Here is a demonstration, so write a piece of data directly, and the user class is also placed directly in.
Json data is returned here. If you want to return xml, you can use Response.AsXml.
4. Moving things (Adapter and SiteRootPath)
If there is a Demo in TinyFox, it will not be posted.
The following is a screenshot of the project
5. Local testing
Before you put it on centos, try to see if it is normal or not!
The port I configured is 12345
The test passed
6. Deploy to centos
Release the project just now and transfer the released file to centos
And then play the role of TinyFox.
. / fox.sh-root / var/www/NancyAPIDemo-p 1234
All right, data service OK.
Let's build a program to access it.
VII. Establish access procedures
Using MVC4 (built by VS2013)
The code that mainly posts the page
1 @ {2 ViewBag.Title = "Index"; 3 Layout = null 4} 5 this is a test to access the data service 6 7 name: 8 9 gender:10 11 12 13 14 $(function () {15 $.ajax ({16 url: 'http://192.168.1.2:1234/getdata',17 dataType:' jsonp' 18 success: function (json) {19 $("# uName") .text (json.uName) 20 $("# uGender") .text (json.uGender); 21} 22}); 23}) 24
Run the access program after we have released it
Is it also very simple and easy!