JSON.NET cast error when serializing Mongo ObjectId

后端 未结 6 1254
旧巷少年郎
旧巷少年郎 2020-11-28 09:36

I am playing around with MongoDB and have an object with a mongodb ObjectId on it. When I serialise this with the .NET Json() method, all is good (but the dates are horrible

6条回答
  •  佛祖请我去吃肉
    2020-11-28 09:53

    I ran into a similar problem with a Web API project, and wound up beating my head against the keyboard for a few hours before I found this thread.

    Initially everything was working fine, but then I ran into the problem after converting my code to use my own custom class instead of the BsonDocument object as recommended in the mongoDB C# driver documentation.

    http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver/#bsondocument-object-model-vs-your-own-domain-classes

    Here the VB.net equivalent to the solution above for those that need it;

    Public Class DocumentMetadata
         _
         _
        Public Property Id() As String
        Public Property Name() As String
        Public Property FullName() As String
    
         _
        Public Property DownloadTime() As DateTime
    End Class
    

提交回复
热议问题