How do I implement a MongoDB Bson formatter in Web API 2?

Adrian Rosca

I have a Web API 2 project and my WebApiConfig looks like this:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config
            .MapHttpAttributeRoutes();


        config
            .Formatters
            .JsonFormatter
            .SupportedMediaTypes
            .Add(new MediaTypeHeaderValue("text/html"));
    }
}

This works fine, but I want the json to be parsed 'Mongo DB style' so automatically get serialization of ObjectIDs, so I can use attributes like [BsonElement("name")] on properties in my model classes and so on.

I looked around and found this article http://odetocode.com/blogs/scott/archive/2013/09/30/custom-serialization-with-json-net-webapi-and-bsondocument.aspx but it seems a bit outdated and I cannot get it to work.

I'm pretty stuck for the moment, help much appreciated.

JotaBe

You need to implement and register a custom media formatter. Basically, you need to do this:

  • derive a class from MediaTypeFormatter (asynchronous read/write implementation) or BufferedMediaTypeFormatter (synchronous read/write implemetation)
    • declare the supported MIME type for BSON format in the class constructor
    • implement CanWriteType + WriteToStream and/or CanreadType + ReadFromStream
  • register the media formatter class in Web API configuration

Take into account that the formatter will be used:

  • to parse the received data, when the media format is specified in Content-Type header (this is the Read part of your implementation)
  • to serialize the returned object when the media format is specified with Accept header (this is the write part of your implementation)

You can have a look at these samples, or google for "Web API custom media formatters" to get more info and samples:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I implement TryGetItemSerializationInfo on a Bson Array Serializer?

From Dev

How do I implement an OAuth2 Authorization_Code Flow in Web Api using OWIN Middleware?

From Dev

How do I implement session aware WEB API on specific controllers?

From Dev

Using a custom JSON formatter for Web API 2

From Dev

How to properly implement asynchronicity in web api 2

From Dev

How do I LINQ nested lists to JSON in Web Api 2?

From Dev

In ASP.Net Web API, how do you change the default formatter for specific Controller or Action

From Dev

How do I group the results of mongodb query and implement in Java?

From Dev

How do I pass the data from MongoDB to my web api through node.js?

From Dev

How do I implement the email service for microsoft web robot

From Dev

How do I implement a web crawler that scrapes ad links?

From Dev

How to implement dependency injection in asp.net web api 2

From Dev

How to Implement the JSONP formatter in ServiceStack

From Dev

How to Implement the JSONP formatter in ServiceStack

From Dev

How to use a specific media formatter with a specifc route in Web Api

From Dev

How can I specify BSON type for coordinates when inserting in MongoDb?

From Dev

How do I extend IdentityRole using Web API 2 + AspNet Identity 2

From Dev

How do I implement my own authentication in Yii2?

From Dev

How do I implement a multicast client in NIO.2?

From Dev

How do I implement simple 2d ball collision?

From Dev

How do we resolve ex.Message = "Unable to cast object of type 'MongoDB.Bson.ObjectId' to type 'MongoDB.Bson.BsonValue'."?

From Dev

How do I find the DateTime formatter pattern for this type of Date?

From Dev

How do I use a custom formatter for json serialization

From Dev

ASP.NET Web API 2: How do I log in with external authentication services?

From Dev

How do i send a "forbidden" response in my Web API 2 solution?

From Dev

How do I get a Google Api OAuth 2 refresh token for my asp.net web application?

From Dev

How do I implement this comparator?

From Dev

How do I implement JDatePicker

From Dev

How do I implement IF in mulesoft

Related Related

  1. 1

    How do I implement TryGetItemSerializationInfo on a Bson Array Serializer?

  2. 2

    How do I implement an OAuth2 Authorization_Code Flow in Web Api using OWIN Middleware?

  3. 3

    How do I implement session aware WEB API on specific controllers?

  4. 4

    Using a custom JSON formatter for Web API 2

  5. 5

    How to properly implement asynchronicity in web api 2

  6. 6

    How do I LINQ nested lists to JSON in Web Api 2?

  7. 7

    In ASP.Net Web API, how do you change the default formatter for specific Controller or Action

  8. 8

    How do I group the results of mongodb query and implement in Java?

  9. 9

    How do I pass the data from MongoDB to my web api through node.js?

  10. 10

    How do I implement the email service for microsoft web robot

  11. 11

    How do I implement a web crawler that scrapes ad links?

  12. 12

    How to implement dependency injection in asp.net web api 2

  13. 13

    How to Implement the JSONP formatter in ServiceStack

  14. 14

    How to Implement the JSONP formatter in ServiceStack

  15. 15

    How to use a specific media formatter with a specifc route in Web Api

  16. 16

    How can I specify BSON type for coordinates when inserting in MongoDb?

  17. 17

    How do I extend IdentityRole using Web API 2 + AspNet Identity 2

  18. 18

    How do I implement my own authentication in Yii2?

  19. 19

    How do I implement a multicast client in NIO.2?

  20. 20

    How do I implement simple 2d ball collision?

  21. 21

    How do we resolve ex.Message = "Unable to cast object of type 'MongoDB.Bson.ObjectId' to type 'MongoDB.Bson.BsonValue'."?

  22. 22

    How do I find the DateTime formatter pattern for this type of Date?

  23. 23

    How do I use a custom formatter for json serialization

  24. 24

    ASP.NET Web API 2: How do I log in with external authentication services?

  25. 25

    How do i send a "forbidden" response in my Web API 2 solution?

  26. 26

    How do I get a Google Api OAuth 2 refresh token for my asp.net web application?

  27. 27

    How do I implement this comparator?

  28. 28

    How do I implement JDatePicker

  29. 29

    How do I implement IF in mulesoft

HotTag

Archive