WebApi接收post方式传入的json数据
[RoutePrefix("Api")]
public class UploadController:BaseApiController
{
[HttpPost]
[Route("Upload")]
public IHttpActionResult Upload()
{
try{
//接收post传入的数据
var request=System.Web.HttpContext.Current.Request;
byte[] requestData=new byte[request.InputStream.Length];
request.InputStream.Read(requestData,0,(int),request.InputStream.Length);
var jsonData=Encoding.UTF8.GetString(requestData);
string JsonData=jsonData.TrimStart('"').TrimEnd('"').Replace(@"\","");
dynamic m=JsonData.ToObject<dynamic>();
//实体化后直接赋值
string MeterNo=m.MeterNo;
string UserCode=m.UserCode;
}
catch(Exception ex){
throw ex;
}
}
public static T ToObject<T>(this string Json)
{
return Json==null?default(T):JsonConvert.DeserializeObject<T>(Json);
}
}