简体   繁体   English

C#500内部服务器错误MongoDB

[英]C# 500 Internal Server Error MongoDB

I've been trying to develop some api controller in ASP.NET that comunicates with mongoDB. 我一直在尝试在ASP.NET中开发一些与mongoDB通信的api控制器。 In same controller I have few post/get methods, and they are working just fine. 在同一个控制器中,我几乎没有post / get方法,而且它们工作得很好。 When I want to update collection in mongoDB, i call post method and when debbuging, all fields in that method are filled, but in return i get 500 error. 当我想更新mongoDB中的集合时,我调用post方法,并且在调试时,该方法中的所有字段都被填充,但是作为回报,我得到了500错误。 Any idea where is the problem? 知道问题出在哪里吗? The code that I use is: 我使用的代码是:

JavaScript 的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 Controller Method 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);
    }

It looks like method is called, but for some reason it returns 500. 它看起来像方法被调用,但由于某种原因它返回500。

If you are using async await pattern in your code, you must always as a best practice return a Task object when the method returns a void ie does not return any object. 如果在代码中使用async await模式,则最佳做法是始终总是在方法返回void时不返回任何对象的情况下返回Task对象。

In your situation, you need to use the following action method that is returning a Task object rather than the original one in which a void was being returned. 在您的情况下,您需要使用以下操作方法,该方法将返回一个Task对象,而不是原来的返回了void对象。

[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);
}

This is according to Microsoft documentation at this URL: Async Await Best Practice 这是根据Microsoft文档的以下URL: 异步等待最佳实践

In the following example, async method Task_MethodAsync doesn't contain a return statement. 在以下示例中,异步方法Task_MethodAsync不包含return语句。 Therefore, you specify a return type of Task for the method, which enables Task_MethodAsync to be awaited. 因此,您可以为该方法指定Task的返回类型,从而可以等待Task_MethodAsync。 The definition of the Task type doesn't include a Result property to store a return value. Task类型的定义不包含用于存储返回值的Result属性。

A code sample from above documentation illustrating this best practice is as given below. 下面是上述文档中说明此最佳实践的代码示例。

    // 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.  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM