3

I've been doing some research on how to get query string parameters from a URL in C# through an API call and I could not find any relevant information on this. I found a bunch of sources on how to do it in PHP but that is not what I'm using.

I also don't know if I have to set up endpoints to accept the query string parameters as part of a call but I believe I do.

All of my Restful API currently works on URL paths so everything that I want to parse through to my backend is separated with a / and I don't want that. I would like to parse all information for processing through query strings and parse only specific path locations using /.

This is how my endpoints are currently set up.

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "api/v1/{controller}/{id?}/{id1?}/{id2?}/{id3?}/{id4?}/{id5?}/{id6?}");
});

This is one of my API calls

[HttpGet("/api/v1/Templates/{customerId}")]
public List<string> GetTemplates(string customerId)
{
    return templatesService.GetTemplates(customerId);
}

I would like to parse my customer ID as http://localhost:0000/api/v1?customerId=1

Example with extra parameters:

[HttpPost("/api/v1/Templates/{customerId}/{templateName}/{singleOptions}/{multiOptions}")]
public string GetReplacedTemplate(string customerId, string templateName, string singleOptions, string multiOptions)
{
    return templatesService.GetReplacedTempalte(customerId, templateName, singleOptions, multiOptions);
}

So here I have 2 extra parameters which are SingleOptions and MultiOptions. Main controller is Templates.

I think even TemplateName should be parse as a Query as I feel like its extra param too.

This is not a problem that is required to solve as using / to separate each parameter works fine but I really want to know how to parse query strings as well.

9
  • http://localhost:0000/api/v1?customerId=1 what would that do? that route would theoretically lead to Index/Home. but what to do there with a customer ID? Commented Jul 13, 2022 at 7:55
  • P.s. you shouldn't use port 0. Altough technically possible, it's officially reserved and should be avoided. Commented Jul 13, 2022 at 7:58
  • You could use public List<string> GetTemplates([FromRoute] string customerId). POST method use [FromBody] to get the request body Commented Jul 13, 2022 at 7:58
  • Isn't Query string params used for parsing data to the backend? How would http://localhost:0000/api/v1?customerId=1 leader to Index/Home. Is there something I'm unaware of with Query strings? Commented Jul 13, 2022 at 7:58
  • @JHBonarius I put port 0000 as example my actual port is different. FYI Commented Jul 13, 2022 at 7:59

1 Answer 1

9

I usually dont answer my own Questions but here it is for others to view.

To take Query string parameters in C# through an API call is actually easy.

Using the [FromQuery] DataBinding on your parameter will allow you to take what ever you parse it :)

[HttpGet("/api/v1/Templates")]
public string GetTemplatesQuery([FromQuery] string customerId)
{
    return customerId;
}
Sign up to request clarification or add additional context in comments.

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.