导入Nancy
Nancy的使用:
代码段:
using Nancy;
using Nancy.Extensions;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace Info
{
public class PersonApi : NancyModule
{
#region 模块对象
private PersonIBLL personBLL = new PersonBLL();
#endregion
public PersonApi ():base("api/BusiInfo/Person")
{
Post["/GetPersonInfo"] = para =>
{
//string postData = Request.Form[""];
string postData = Request.Body.AsString();
dynamic info = JsonConvert.DeserializeObject(postData);
Person StationList = personBLL .GetEntity(info.ID.ToString());
// 返回登录结果对象集
return Response.AsJson(StationList);
};
Post["/GetList"] = para =>
{
//string postData = Request.Form[""];
string postData = Request.Body.AsString();
dynamic info = JsonConvert.DeserializeObject(postData);
IEnumerable<PersonEntity> StationList = personBLL.GetList(info.PERSONNO.ToString(), info.PERSONTYPE.ToString());
// 返回登录结果对象集
return Response.AsJson(StationList);
};
}
}
}
使用postman来测试发送请求:
使用postman时,nancy接收请求用
string postData = Request.Form[""];
而不用:string postData = Request.Body.AsString();
当返回数据量多时,返回用return Response.AsText(json),但是需要将数据转化成json格式;
本文介绍如何在Nancy框架中接收并处理来自Post请求的JSON字符串。通过`Request.Body.AsString()`获取JSON数据,使用Newtonsoft.Json进行反序列化,并展示了如何返回JSON响应。特别指出,在Postman测试时,正确读取请求体的方式是`Request.Body.AsString()`而非`Request.Form[""]`。

666

被折叠的 条评论
为什么被折叠?



