zoukankan      html  css  js  c++  java
  • Nancy 简单学习

    1. 环境 vs2010 , nancy 

    2. 需要测试的功能

       1. 输出

         页面输出简单文本

       2. 输出json 数据

    3. 操作

        1. 创建asp.net 空项目

        2. 添加引用Nancy库 使用nuget

        如图:

       

      4. 编码

         创建模块: UserLoginModel

         

    public class UserLoginModel:NancyModule
    {

    public UserLoginModel()
    {
    Get["/login"] = _=> "dalong";   //  输出dalong
    Get["/App"] = parameter =>
    {
    var usermodel = new UserModel { Age = 555, Name = "dalong" };   // 返回(按照客户请求的模式)
    return Negotiate.WithStatusCode(HttpStatusCode.OK)
    .WithModel(usermodel);
    };
    Get["/App/{id}"] = parameter =>     // 根据获取的参数创建集合,并返回数据(按照客户请求的模式)
    {
    // var usermodel = new UserModel { Age = 555, Name = "dalong" };
    var id = parameter.id;
    List<UserModel> list = CreateUserList.CreateList(id);
    return Negotiate.WithStatusCode(HttpStatusCode.OK).WithModel(list);
    };

    }
    }

             UserModel :

    public class UserModel
    {

    public int Age { get; set; }

    public string Name { get; set; }
    }

              CreateUserList:

    public class CreateUserList
    {

    public static List<UserModel> CreateList(int Startindex)
    {
    List<UserModel> list = new List<UserModel>();

    for (int i = Startindex; i < Startindex+10; i++)
    {

    list.Add(new UserModel { Name = "dalong" + i, Age = i });
    }
    return list;

    }
    }

    5. 测试效果:

         json 格式的测试使用dev http client 

         截图如下:

         

  • 相关阅读:
    大数据基本概念及Hadoop技术基础
    基于 ReliefF和K-means算法的应用
    利用Hadoop和Spark处理用户心跳周期数据
    Java线程池源码解析及高质量代码案例
    muleESB的第一个开发实例-HelloWorld(二)
    [USACO11JAN]道路和飞机Roads and Planes
    CH6101 最优贸易
    POJ3662 Telephone Lines
    扫描线+线段树例题(HDU1542)
    关于Dinic算法的几点讨论
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/4182279.html
Copyright © 2011-2022 走看看