Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Nancy combined with tinyfox provides simple data services for our applications.

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report