public static class HttpRequestExtension { /// <summary> /// 依据Request Body构建指定的实体类型 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static T ReadEntity<T>(this HttpRequestBase request) where T : class { if (request.InputStream.Length == 0) throw new Exception("request body must be not null"); try { DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(T)); request.InputStream.Position = 0; return jsonSerializer.ReadObject(request.InputStream) as T; } catch (SerializationException) { return null; } } }
作者:协思
出处:http://zeeman.cnblogs.com/
QQ交流群:32972862