我是Web api的新手,我似乎无法通过POST从.net项目发送对象。
以下是发送对象的代码:
public async Task<bool> CreateUser (User user)
{
Uri uri = new Uri(apiUrl + "api/users");
var jsonUser = JsonConvert.SerializeObject(user);
HttpContent content = new StringContent(jsonUser, Encoding.UTF8, "application/json");
var postUser = await client.PostAsync(uri, content);
string result = await postUser.Content.ReadAsStringAsync();
if (postUser.IsSuccessStatusCode)
{
return true;
}
return false;
}
这是Web API代码:
[HttpPost]
public IHttpActionResult Post([FromBody]string jsonUser)
{
UserDTO user = JsonConvert.DeserializeObject<UserDTO>(jsonUser);
bool tryCreate = _userRepository.CreateUser(user);
if (tryCreate == false)
{
return NotFound();
}
return Ok();
}
如果我尝试将POST请求发送到Web api URL,它将成功将json字符串发送到该动作,但是如果我尝试从Web项目发送POST数据,则Content.ReadAsStringAsync()
返回:>
"{"Message":"An error has occurred.","ExceptionMessage":"Value cannot be null.\r\nParameter name: value","ExceptionType":"System.ArgumentNullException","StackTrace":" at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)\r\n at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)\r\n at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)\r\n at Travue.WebAPI.Controllers.UsersController.Post(String jsonUser) in C:\\Users\\Dev01\\Documents\\projects\\travue\\Travue.WebAPI\\Controllers\\UsersController.cs:line 46\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.<GetExecutor>b__2(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()"}"