ASP.NET MCV4とMongoDBはかなり新しく、WebAPIを構築しようとしています。私はついにそれを正しく理解したと思いましたが、アプリを起動して次のように入力http://localhost:50491/api/document
すると、ブラウザにこのエラーメッセージが表示されます
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
これが私のコードです
これはドキュメントクラスです
public class Document
{
[BsonId]
public ObjectId DocumentID { get; set; }
public IList<string> allDocs { get; set; }
}
これは、DBへの接続が行われる場所です。
public class MongoConnectionHelper
{
public MongoCollection<BsonDocument> collection { get; private set; }
public MongoConnectionHelper()
{
string connectionString = "mongodb://127.0.0.1";
var server = MongoServer.Create(connectionString);
if (server.State == MongoServerState.Disconnected)
{
server.Connect();
}
var conn = server.GetDatabase("cord");
collection = conn.GetCollection("Mappings");
}
ApiControllerクラスは次のとおりです。
public class DocumentController : ApiController
{
public readonly MongoConnectionHelper docs;
public DocumentController()
{
docs = new MongoConnectionHelper();
}
public IList<BsonDocument> getAllDocs()
{
var alldocs = (docs.collection.FindAll().ToList());
return alldocs;
}
}
さらに読み進めると、エラーメッセージが表示されます。
Type 'MongoDB.Bson.BsonObjectId' with data contract name 'BsonObjectId:http://schemas.datacontract.org/2004/07/MongoDB.Bson' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
それはすべて良いことですが、どうすればよいですか?