【发布时间】:2017-05-19 12:29:54
【问题描述】:
我在 Visual Studio 2010 中使用向导创建了 API 控制器。
它创建了这个函数:
// POST api/Default1
public HttpResponseMessage PostUserProfile(UserProfile userprofile)
{
System.Diagnostics.Debug.WriteLine("Username: " + userprofile.UserName);
if (ModelState.IsValid)
{
db.UserProfiles.Add(userprofile);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, userprofile);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = userprofile.UserId }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
我无法添加 [HttpPost] 注释,因为我猜 MVC 4 使用较旧的 API。我还得到参数 userprofile 的 NullPointerException,这意味着它的数据类型错误。我的 PostUserProfile() 函数的 send as 参数应该如何更改以及如何更改? 应该是 Json 对象还是我不知道是什么?
【问题讨论】:
标签: asp.net-mvc rest asp.net-mvc-4 model-view-controller