图2
public class Response
{
public int status { get; set; }
public string msg { get; set; }
public object data { get; set; }
}
public class PageData
{
public int totalnum { get; set; }
public int totalpage { get; set; }
public int currentpage { get; set; }
public object info { get; set; }
}
第三、YBaseFramework.YBF.HelloWorldApiServices此项目为WebApi具体实现,目前HelloWorld为名,仅是一个demo,文件结构如下:
图3
在上面的引擎中,我已经将路由的定义,标红色文字,所以这里的DemoDBServices的文件夹的命名就是必须以DBServices结束,控制器的命令也是以Controller结束,只好mvc中的控制器命名一样;
[HttpGet]
public Response GetUsers()
{
try
{
var res = new Response();
res.status = 0;
res.msg = "success";
var data = new PageData();
data.currentpage = 1;
data.totalnum = 100;
data.totalpage = 10;
var infos = new List<dynamic>();
for (int i = 1; i < 1000; i++)
{
infos.Add(new { Name = "abc" + i.ToString(), ID = i });
}
data.info = infos;
res.data = data;
_context.InfoFormat(GetType(), "HelloWorldController {0} 处理数据条数:{1}", Utils.GetVersion(), infos.Count);
return res;
}
catch (Exception ex)
{
_context.ErrorFormat(GetType(), "HelloWorldController {0} 错误原因:{1}", Utils.GetVersion(),ex.Message);
return null;
}
}
/// <summary>
/// post测试验证登录
/// </summary>
/// <param name="memberModel"></param>
/// <returns></returns>
[HttpPost]
public Response LoginCheck([FromBody]MEMBERModel memberModel)
{
var obj = RequestContext.RouteData.Values;//取路由的信息
string Url = RequestContext.Url.Request.RequestUri.LocalPath;
Response res = ParamVerificationHelper.CheckParVerLegal<MEMBERModel>(Url, memberModel);
if (res != null)
{
return res;
}
string msg = string.Empty;
string state = "0";
if(string.IsNullOrWhiteSpace(memberModel.USERNAME))
{
state = "3";
}
else if(string.IsNullOrWhiteSpace(memberModel.PASSWORD))
{
state = "2";
}
switch (state)
{
case "0":
msg = "登录成功";
break;
case "1":
msg = "审核不通过";
break;
case "2":
msg = "密码不正确";
break;
case "3":
msg = "帐号不存在";
break;
}
object data = null;
if (state == "0")
{
data =
new
{
username = memberModel.USERNAME,
phone = "13761085465-app",
email = "baishi_12@163.com",
image =
string.IsNullOrEmpty("")
? "http://www.gjw.com/image/face/1.jpg"
: "http://img0.gjw.com/face/" + "",
name = "baishi",
gender = "男" == "女" ? "2" : "1",
password = ""
};
}
var response = new Response { status = state == "0" ? 0 : 1, msg = msg, data = data };
return response;
}
在DemoModel文件夹中是定义了具体接口所要使用到的实体:
ParamModel.cs为接口接收参数部分
/// <summary>
/// 前台会员信息
/// </summary>
public class MEMBERModel
{
public string USERNAME { get; set; }
public string PASSWORD { get; set; }
public string vastr { get; set; }
}
/// <summary>
/// 商品信息
/// </summary>
public class PRODUCTModel
{
public string vastr { get; set; }
}
Product.cs
public class Product
{
public string Name { get; set; }
public string Price { get; set; }
}
以上三个大步,就可以实现了两个接口,下面我们就以Demo的操作图片说明。
第四、发布测试接口
A、接口测试配置文件:
<root>
<Interface name="GetUsers" url="http://localhost:3721/hw/demo/HelloWorld/getusers.html" params="" type="get" remark="取用户列表接口说明"></Interface>
<Interface name="Getlist" url="http://localhost:3721/hw/demo/HelloWorld/getlist.html" params="" type="get" remark="商品列表接口说明"></Interface>
<Interface name="LoginCheck" url="http://localhost:3721/hw/demo/HelloWorld/LoginCheck1.html" params="USERNAME={0}&PASSWORD={1}" type="post" remark="登录接口说明"></Interface>
</root>
B、接口实体实现宿主程序,如图: