1

I have an application where there will be several parameters passed to my endpoint for searching, these parameters are not defined because they are dynamically generated so i cannot map it to a specific model. What would be the best way to map any query parameters into my GET endpoint?

[HttpGet]
public CustomResponse GetResults({something here that will map the parameters?})
{
    //perhaps a dictionary? a collection of some sort? 
}

Then I need to get all those keys and values and search the database for anything containing that and as i said it could be anything.

So I could pass something like?

/api/Merchandise/GetResults?sku=30021&cupsize=medium&color=red&location=south& {and all the dynamic fields which could be anything}

2 Answers 2

4

HttpRequest object has Query property that is an IQueryCollection and holds all passed query parameters.

In other words, in your action method you may do:

[HttpGet]
public CustomResponse GetResults()
{
    var queryParams = HttpContext.Request.Query;

    // directly get by name
    var value1 = queryParams["parameter_name"];
    // or queryParams.TryGetValue()

    foreach (var parameter in queryParams)
    {
        string name = parameter.Key;
        object value = parameter.Value;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could map it to JObject, which is like a Dictionary.

Don't forget:

using Newtonsoft.Json;

Comments

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.