在 Web 中执行 PUT 时出现 NullObjectReference Api
NullObjectReference when performing a PUT in Web Api
我正在尝试更新 class "Conversation" 中的变量,因为某些原因,参数对象未绑定。我不知道为什么不。结果我收到这个错误:
"Object reference not set to an instance of an object.",
控制器:
[HttpPut]
public async Task<IHttpActionResult> AddMember(string key, User user)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Conversation conver = await db.Conversations.FindAsync(key); //THIS IS NULL
if (conver == null)
{
return NotFound(); //METHOD IS ENDING HERE
}
conver.Members.Add(user); //THIS IS NULL
db.Entry(conver).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
return NotFound();
}
return StatusCode(HttpStatusCode.NoContent);
}
型号class用户:
namespace AcademicAssistant.Models
{
[DataContract]
public class User
{
[Key]
[DataMember]
public string Email { get; set; }
[DataMember]
public string Password { get; set; }
[DataMember]
public bool Admin { get; set; }
}
}
模特Class对话:
[DataContract]
public class Conversation
{
[Key]
[DataMember]
public string Key { get; set; }
[DataMember]
public string ConversationName { get; set; }
[DataMember]
public string Administrator { get; set; }
[DataMember]
public virtual ICollection<User> Members { get; set; }
[DataMember]
public virtual ICollection<Message> Messages { get; set; }
}
我正在 Postman 中这样测试它:
路线 = api/Conversations/AddMember/?key="1LM4"
JSON = {"user": { "Email": "56@56.com", "Password" : "Passw-1", "Admin" : true } }
解决方案:
路线应该是
api/Conversations/AddMember/?key=1LM4
和 JSON 格式如下:
{
"Email": "56@56.com",
"Password" : "Passw-1",
"Admin" : true
}
我正在尝试更新 class "Conversation" 中的变量,因为某些原因,参数对象未绑定。我不知道为什么不。结果我收到这个错误:
"Object reference not set to an instance of an object.",
控制器:
[HttpPut]
public async Task<IHttpActionResult> AddMember(string key, User user)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Conversation conver = await db.Conversations.FindAsync(key); //THIS IS NULL
if (conver == null)
{
return NotFound(); //METHOD IS ENDING HERE
}
conver.Members.Add(user); //THIS IS NULL
db.Entry(conver).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
return NotFound();
}
return StatusCode(HttpStatusCode.NoContent);
}
型号class用户:
namespace AcademicAssistant.Models
{
[DataContract]
public class User
{
[Key]
[DataMember]
public string Email { get; set; }
[DataMember]
public string Password { get; set; }
[DataMember]
public bool Admin { get; set; }
}
}
模特Class对话:
[DataContract]
public class Conversation
{
[Key]
[DataMember]
public string Key { get; set; }
[DataMember]
public string ConversationName { get; set; }
[DataMember]
public string Administrator { get; set; }
[DataMember]
public virtual ICollection<User> Members { get; set; }
[DataMember]
public virtual ICollection<Message> Messages { get; set; }
}
我正在 Postman 中这样测试它:
路线 = api/Conversations/AddMember/?key="1LM4"
JSON = {"user": { "Email": "56@56.com", "Password" : "Passw-1", "Admin" : true } }
解决方案:
路线应该是
api/Conversations/AddMember/?key=1LM4
和 JSON 格式如下:
{
"Email": "56@56.com",
"Password" : "Passw-1",
"Admin" : true
}