JSON response through .NET

I am working with .NET to fetch data from MongoDb.

When the below API method is called through the 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);
        }

the response has Content-Type as text/plain. I need the Content-Type as application/json.

Any suggestions?

Maybe you can try the following codes:

var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };

var firstStudent= document.ToJson(jsonWriterSettings);

MongoDB.Bson (2.5+) has support to map between BsonValues and .Net objects. BsonTypeMapper Class

To map a BsonValue (or BsonDocument) to .Net object use:

var dotNetObj = BsonTypeMapper.MapToDotNetValue(bsonDoc);

You can then use your choice of serialization library:

var jsObj = JsonConvert.SerializeObject(dotNetObj);