0

I am looking a way to send a mongodb filter from the webapp threw http to the web api.

In the webapp controller: serialize or deserialize then Encrypt

var filter = Builder<BsonDocument>.filter.Eq("firstName", "Foo");

In the web api controller: Decrypt, serialize or deserialize

The logic between is working.

Should I receive it as a query in the web api controller

    [HttpGet]
    public async Task<IActionResult> GetByQuery([FromQuery] string filter)
    {
    }

I am not sure how to send the mongodb filter, I did some research but still not able to make it work.

I hope is clear! Thanks

Mongodb 2.4.4
Asp.net core 2.0

Update
What I am try to do is for example:
http://localhost:12345/api/v1/monsters/ "the mongodb filter here"
or
http://localhost:12345/api/v1/monsters?filter= "mongodb filter here as query string"

How to or what I have to do and what is the best way to send a MongoDB filter. Should I have a filterdefinition and serialized to json or vice versa, I am looking for the best approach.

3
  • I'm not sure if you're asking about how to call the Web API, but if so you can read the examples at the bottom of this page - HttpClient Class Commented Oct 15, 2017 at 2:39
  • I just update my question, the http part I am ok. Is juste I am not sure how can I send the mongodb filter, as a string or as a query, what is the step to serialize the filter if needed... Commented Oct 15, 2017 at 3:01
  • Typically one would send something more semantic over the http wire, then convert that to a db filter accordingly on the backend. There's numerous advantages, both in terms of security and long term maintainability of the system. Instead of passing a filter string, consider passing key/value pairs and parsing those into a database filter closer to your data access code. Commented Oct 17, 2017 at 17:24

1 Answer 1

1

To get the URL, I had to check MongoDB Json Generation.

var json = filter.Render(BsonDocumentSerializer.Instance, BsonSerializer.SerializerRegistry).ToString();
var url = $"http://localhost:12345/api/v1/monsters?filter={WebUtility.UrlEncode(json)}";

Then in your API:

var mongoFilter = (FilterDefinition<BsonDocument>)WebUtility.UrlDecode(filter);

To answer your question of whether this is the best approach, I might need to know a bit more about your application architecture and the complexity of your application.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jaybird ! Sorry for the delay ! This did the trick but my approch is not the best one I should use Post method not Get. I could update your answer in the future, I'm still working on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.