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.

Sinan
  • 898
  • 1
  • 9
  • 23
  • 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](https://msdn.microsoft.com/en-us/library/system.net.http.httpclient) – Jaybird Oct 15 '17 at 02: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... – Sinan Oct 15 '17 at 03: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. – jleach Oct 17 '17 at 17:24

1 Answers1

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.

Jaybird
  • 541
  • 4
  • 13
  • 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. – Sinan Nov 27 '17 at 22:26