【发布时间】:2020-03-19 12:33:14
【问题描述】:
当通过 API 调用下面的 API 方法时
public IActionResult FirstStudent()
{
var collection = this.database.GetCollection<BsonDocument>("students");
var filter = Builders<BsonDocument>.Filter.Eq("RollNo", "1");
var document = collection.Find(filter).First();
var firstStudent= document.ToJson();
return Ok(firstStudent);
}
响应的 Content-Type 为 text/plain。 我需要 Content-Type 作为 application/json。
有什么建议吗?
【问题讨论】:
-
试试
return JsonResult(firstStudent) -
我收到错误:不可调用的成员 'JsonResult' 不能像方法一样使用。
-
我的错,试试
return new JsonResult(firstStudent) -
我得到 "{ \"_id\" : \"5e54f99a045dba534831a1a2\", \"RollNo\" : \"1\", \"name\" : \"Johnson\"}"我更想要 { "_id" : "5e54f99a045dba534831a1a2", "RollNo" : "1", "name" : "Johnson"}
-
在下面添加了一个有效的答案
标签: c# mongodb asp.net-core-webapi mongodb-.net-driver webapi