我在Visual Studio 2017下工作。
我想检索用户在表单中键入的评论。为此,如果数据库中没有数据,则使用发布请求,如果有数据,则使用放置请求。该帖子正在运行,但是当有数据时,放置权返回错误System.NullReferenceException:实体为null。
这是我正在使用的代码:
Commentaire表控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MeteoDataAccess;
namespace MeteoCSSD___API.Controllers
{
public class CommentaireController : ApiController
{
public IEnumerable<Commentaire> Get()
{
using (MeteoCSSDEntities entities = new MeteoCSSDEntities())
{
return entities.Commentaire.ToList();
}
}
public Commentaire Get(int id)
{
using (MeteoCSSDEntities entities = new MeteoCSSDEntities())
{
return entities.Commentaire.FirstOrDefault(e => e.Id == id);
}
}
public void Post([FromBody]Commentaire commentaire)
{
if (commentaire == null)
{
throw new ArgumentNullException(nameof(commentaire));
}
using (MeteoCSSDEntities entities = new MeteoCSSDEntities())
{
entities.Commentaire.Add(commentaire);
entities.SaveChanges();
}
}
public void Put(int id, [FromBody]MeteoDataAccess.Commentaire commentaire)
{
if (commentaire == null)
{
throw new ArgumentNullException(nameof(commentaire));
}
using (MeteoCSSDEntities entities = new MeteoCSSDEntities())
{
var entity = entities.Commentaire.FirstOrDefault(e => e.Id == id);
entity.Commentaire1 = commentaire.Commentaire1;
entities.SaveChanges();
}
}
public HttpResponseMessage Options()
{
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK
};
return response;
}
}
}
前端函数调用请求:
CheckCommentaire(){
if (this.commentaireList.length === 0 ) {
if (this.comm1.Commentaire1 != null) {
this.sqlP.postCommentaire(this.comm1);
}
if (this.comm2.Commentaire1 != null) {
this.sqlP.postCommentaire(this.comm2);
}
if (this.comm3.Commentaire1 != null) {
this.sqlP.postCommentaire(this.comm3);
}
if (this.comm4.Commentaire1 != null) {
this.sqlP.postCommentaire(this.comm4);
}
if (this.comm5.Commentaire1 != null) {
this.sqlP.postCommentaire(this.comm5);
}
} else {
if (this.comm1.Commentaire1 != null) {
this.sqlP.putCommentaire(this.comm1.IdService, this.comm1);
}
if (this.comm2.Commentaire1 != null) {
this.sqlP.putCommentaire( this.comm2.IdService, this.comm2);
}
if (this.comm3.Commentaire1 != null) {
this.sqlP.putCommentaire(this.comm3.IdService, this.comm3);
}
if (this.comm4.Commentaire1 != null) {
this.sqlP.putCommentaire( this.comm4.IdService, this.comm4);
}
if (this.comm5.Commentaire1 != null) {
this.sqlP.putCommentaire( this.comm5.IdService, this.comm5);
}
}
}
这是Visual Studio返回的错误:
System.NullReferenceException
HResult=0x80004003
Message=La référence d'objet n'est pas définie à une instance d'un objet.
Source=MeteoCSSD - API
StackTrace:
at MeteoCSSD___API.Controllers.CommentaireController.Put(Int32 id, Commentaire commentaire) in J:\GITLil\MeteoCSSD-API\MeteoCSSD - API\Controllers\CommentaireController.cs:line 57
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_1.<GetExecutor>b__0(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)