创建web api控制器 添加Fluent Nhibernate插件
以下为服务器
using
System;
using
System.Collections.Generic;
using
System.Linq;
usingSystem.Net;
usingSystem.Net.Http;
using
System.Web.Http;
using
HttpWebServer0501.Models;
using
LitJson;
namespace
HttpWebServer0501.Controllers
{
///
<summary>
///
弱联网 http
///
</summary>
public
class
ValuesController
: ApiController
{
//创建字典 设置键值对 键为协议号 值为消息类
Dictionary<
int
, MsgHandler> dic =
new
Dictionary<
int
, MsgHandler>();
public
ValuesController()
{
Init();
}
private
void
Init()
{
//实例化模块 登录/注册
LoginModels login =
new
LoginModels();
//添加到字典中 键为协议号int,值为LoginModels的Login方法
dic.Add(10001, login.Login);
dic.Add(10002, login.Register);
}
// GET api/values
public
IEnumerable<
string
> Get()
{
return
new
string
[] {
"value1"
,
"value2"
};
}
// GET api/values/5
public
string
Get(
int
id)
{
return
"value"
;
}
// POST api/values
///
<summary>
///
转json
///
拿到协议号,转为int类型
///
尝试去获取值 传入键值
///
判断值不为空返回消息 传入json
///
</summary>
///
<param name="
value
"></param>
///
<returns></returns>
public
string
Post([FromBody]
string
value)
{
//解析json
JsonData data = LitJson.JsonMapper.ToObject(value);
//拿到协议号转为int类型
int
protocol =
int
.Parse(data[
"protocol"
].ToString());
MsgHandler msg;
//尝试取值
dic.TryGetValue(protocol,
out
msg);
//判断消息类对象不为空时返回json消息 否则就返回null
if
(msg !=
null
)
{
return
msg(data);
}
return
null
;
}
// PUT api/values/5
public
void
Put(
int
id, [FromBody]
string
value)
{
}
// DELETE api/values/5
public
void
Delete(
int
id)
{
}
}
}
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
namespace
HttpWebServer0501.Models
{
///
<summary>