Search code examples
c#asp.net-core-mvcbson

How to check if string is BSON in C# .net core?


I have login method:

[HttpPost("login")]
public ResponseBody<UserContext> Login([FromBody] RequestBody<Operator> userCredentials)
{}

RequestBody have T Data field.

is it possible to convert this method to be able to handle both JSON and BSON?


Solution

  • Add BSON support to your API, this artcile should help. And based on request type API can supprort both JSON and BSON.

    From article:

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Formatters.Add(new BsonMediaTypeFormatter());
    
            // Other Web API configuration not shown...
        }
    }
    

    Now if the client requests "application/bson", Web API will use the BSON formatter.