<
Loading Image...
>
I am trying to retireve all the documents in my MongoDB collection via a
REST api built in ASP.NET MVC 4 and I have encoutered an error when i enter
localhost:50491/api/document:
An error occurred while deserializing the Id property of class
Acord_Rest_API.Models.Document: Cannot deserialize string from BsonType
ObjectId.
My controller looks like:
public class DocumentController : ApiController
{
public readonly MongoConnectionHelper<Document> docs;
public DocumentController()
{
docs = new MongoConnectionHelper<Document>();
}
public IList<Document> getAllDocs()
{
var alldocs = docs.collection.FindAll();
return alldocs.ToList();
}
My MongoDB document looks like this?
[1]:
Loading Image...
What am I missing here?
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
WebApi doesn't know how to serialize and deserialize an ObjectId. You have
two options... I'd suggest #2 because it's easier and has the benefit of
not coupling your entities to MongoDB.
1) Right a converter for WebApi and register it.
2) Change your Document model to use something else as the identifier type.
public class Document
{
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set;}
}
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Sorry, hit enter accidentally.
You can also register this via the configuration api if you'd like.
Another alternative is to use Guids as your identifier. However, if you go
that route you will NOT be able to use the aggregation framework.
Post by craiggwilson
WebApi doesn't know how to serialize and deserialize an ObjectId. You have
two options... I'd suggest #2 because it's easier and has the benefit of
not coupling your entities to MongoDB.
1) Right a converter for WebApi and register it.
2) Change your Document model to use something else as the identifier type.
public class Document
{
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set;}
}
--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.