1

I am trying to create an azure function app with two http trigger functions in it with open API specs, but both using same route but with different parameters.

Requirement to fetch some templates from Database. first way is when customer provides id, another way when customer provides name

Here is my function looks like

[FunctionName("GetTemplateById")]
[OpenApiOperation(operationId: "Get-Template", tags: new[] { "Get-Template" })]
[OpenApiParameter(name: "id", In = ParameterLocation.Query, Required = true, Description = "template id")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(JObject), Description = "The OK response")]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.Unauthorized, Summary = "Unauthorized Access", Description = "Unauthorized Access.")]
public async Task<IActionResult> GetTemplateById(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "template")] HttpRequest req)
{
    //some code using id parameter

    return new ObjectResult(response);
}

But I need another function where to fetch template by template name instead of ID. But how can I make it with the same route, but different parameter. I am not thinking about a solution where both parameters in single function and making both optional. Any other workaround?

2 Answers 2

1

In order to follow REST api best practices i would recommand the following routes/url

http://yourApi/templates/{templateid}

to get a single template based on its key (id)

http://yourApi/templates?templateName='searchedName'

to get all templates, with possibility to filter on differents properties, in this case filtered on templateName

have a look at REST Resource Naming Guide

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

Comments

0

Attribute Routing, specifically Route Constraints should allow you to do this in a way that's highly explicit about what route will be selected given the input.

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.