Javascript C#500内部服务器错误MongoDB

Javascript C#500内部服务器错误MongoDB,javascript,c#,asp.net,mongodb,Javascript,C#,Asp.net,Mongodb,我一直在尝试在ASP.NET中开发一些与mongoDB通信的api控制器。在同一个控制器中,我有几个post/get方法,它们工作得很好。当我想更新mongoDB中的集合时,我调用post方法,当去bug时,该方法中的所有字段都被填充,但作为回报,我得到了500个错误。知道问题出在哪里吗?我使用的代码是: JavaScript comment.id += id; comment.comment += test; var newCommentUrl = 'api/PostInfo/' + comm

我一直在尝试在ASP.NET中开发一些与mongoDB通信的api控制器。在同一个控制器中,我有几个post/get方法,它们工作得很好。当我想更新mongoDB中的集合时,我调用post方法,当去bug时,该方法中的所有字段都被填充,但作为回报,我得到了500个错误。知道问题出在哪里吗?我使用的代码是:

JavaScript

comment.id += id;
comment.comment += test;
var newCommentUrl = 'api/PostInfo/' + comment;
postDataToDatabase(comment, newCommentUrl);

function postDataToDatabase(data, url) {
$.ajax({
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    url: url,
    type: 'POST',
    contentType: 'application/json;',
    data: JSON.stringify(data),
    success: function (valid) {
        if (valid) {
        } else {
        }
    }
});
ASP.NET控制器方法

        [HttpPost]
    [Route("api/PostInfo/{Comment}")]
    public async void Post(Comment comment)
    {
        BsonObjectId oldId = new BsonObjectId(new ObjectId(comment.id.ToString()));

        var mongoDbClient = new MongoClient("mongodb://127.0.0.1:27017");
        var mongoDbServer = mongoDbClient.GetDatabase("nmbp");
        var collection = mongoDbServer.GetCollection<PostInfo>("post");

        var filter = Builders<PostInfo>.Filter.Eq(e => e._id, oldId);
        var update = Builders<PostInfo>.Update.Push("post_comments", comment.comment);
        await collection.FindOneAndUpdateAsync(filter, update);
    }
[HttpPost]
[路由(“api/PostInfo/{Comment}”)]
公共异步void Post(注释)
{
BsonObjectId oldId=新的BsonObjectId(新的ObjectId(comment.id.ToString());
var mongoDbClient=新的MongoClient(“mongodb://127.0.0.1:27017");
var mongoDbServer=mongoDbClient.GetDatabase(“nmbp”);
var collection=mongoDbServer.GetCollection(“post”);
var filter=Builders.filter.Eq(e=>e.\u id,oldId);
var update=Builders.update.Push(“post_comments”,comment.comment);
wait collection.FindOneAndUpdateAsync(筛选、更新);
}

它看起来像是调用了方法,但由于某种原因,它返回500。

如果您在代码中使用
异步等待
模式,那么最好的做法是,当方法返回一个空值(即不返回任何对象)时,您必须始终返回一个
任务
对象

在您的情况下,您需要使用以下操作方法返回
任务
对象,而不是返回
无效
的原始对象

[HttpPost]
[Route("api/PostInfo/{Comment}")]
public async Task Post(Comment comment)
{
    BsonObjectId oldId = new BsonObjectId(new ObjectId(comment.id.ToString()));

    var mongoDbClient = new MongoClient("mongodb://127.0.0.1:27017");
    var mongoDbServer = mongoDbClient.GetDatabase("nmbp");
    var collection = mongoDbServer.GetCollection<PostInfo>("post");

    var filter = Builders<PostInfo>.Filter.Eq(e => e._id, oldId);
    var update = Builders<PostInfo>.Update.Push("post_comments", comment.comment);
    await collection.FindOneAndUpdateAsync(filter, update);
}

500表示服务器出现问题。我建议您将服务器端代码包装在try-catch块中,如果无法在调试器中单步执行代码,则将任何异常记录到文件或其他持久存储中。问题是没有引发异常。在调试器中一步一步地执行时,会填充所有变量。在控制器方法中,您能否将方法的第一行更改为
公共异步任务帖子(注释)
,这样您就不会从异步方法返回空值,而是从任务对象返回空值?我认为这应该行得通,但最好是你在自己的情况下尝试一下。@Sunil你能把它作为答案,这样我就能给它打分吗?这就是问题所在,有时候你看不到树林里的树。非常感谢。好的,当然可以。给我几分钟。
    // TASK EXAMPLE
async Task Task_MethodAsync()
{
    // The body of an async method is expected to contain an awaited 
    // asynchronous call.
    // Task.Delay is a placeholder for actual work.
    await Task.Delay(2000);
    // Task.Delay delays the following line by two seconds.
    textBox1.Text += String.Format("\r\nSorry for the delay. . . .\r\n");

    // This method has no return statement, so its return type is Task.  
}