当Nancy版本为2.0.0.0时
string postData = Request.Body.AsString;
当Nancy版本为1.4.5.0时
自己写一个扩展方法,代码如下
/// <summary>
/// Extensions for Stream
/// </summary>
public static class StreamExtensions
{
internal const int BufferSize = 4096;
//
// 摘要:
// Gets the request body as a string.
// 参数:
// stream:
// The request body stream.
//
// encoding:
// The encoding to use, System.Text.Encoding.UTF8 by default.
//
// 返回结果:
// The request body as a System.String.
public static string AsString(this Stream stream, Encoding encoding = null)
{
using (StreamReader streamReader = new StreamReader(stream, encoding ?? Encoding.UTF8, true, BufferSize))
{
if (stream.CanSeek)
{
long position = stream.Position;
stream.Position = 0L;
string result = streamReader.ReadToEnd();
stream.Position = position;
return result;
}
}
return string.Empty;
}
}
使用
string postData = Request.Body.AsString();