Is it safe to always use @{} instead of @()in APIM policy expressions

Martin Kallukalam 170 Reputation points
2024-06-29T14:11:57.8733333+00:00

The APIM doc says
Multi-statement expressions:

  • Enclosed in @{expression}.
  • All code paths within multi-statement expressions must end with a return statement.

However there are examples in APIM doc, where {} is used where () made sense.
eg: https://video2.skills-academy.com/en-us/azure/api-management/api-management-sample-send-request

I am confused why the { } was used instead of ( )
why this --> from={(string)context.Variables["fromDate"]}&to={(string)context.Variables["fromDate"]}
why not this --> from=((string)context.Variables["fromDate"]}&to={(string)context.Variables["fromDate"])

Below snippet from above doc link

  <send-request mode="new" response-variable-name="revenuedata" timeout="20" ignore-error="true">
    <set-url>@($"https://accounting.acme.com/salesdata?from={(string)context.Variables["fromDate"]}&to={(string)context.Variables["fromDate"]}")</set-url>
    <set-methodGET</set-method>
  </send-request>
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,901 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pinaki Ghatak 2,720 Reputation points Microsoft Employee
    2024-06-30T15:14:11.4633333+00:00

    Hello @Martin Kallukalam

    This is a great question, and allow me to address it.

    In Azure API Management (APIM) policy expressions, @{} and @() are used for different purposes:

    • @() is used for single statement expressions, where the expression is a well-formed C# expression statement.
    • @{} is used for multi-statement expressions. All code paths within multi-statement expressions must end with a return statement.

    In the example you provided, the {} inside the $"" string is not an APIM policy expression, but rather, it’s part of the C# string interpolation syntax. In C#, $"" is used to create strings where you can embed expressions inside {} that will be replaced with their values when the string is created.

    So, in the line:

    @($"https://accounting.acme.com/salesdata?from={(string)context.Variables["fromDate"]}&to={(string)context.Variables["fromDate"]}")
    
    

    The {} are not APIM policy expressions, but placeholders for the C# string interpolation. The (string)context.Variables["fromDate"] inside {} is a C# expression that gets the value of the "fromDate" variable from the context.Variables dictionary and casts it to a string. This value then replaces {(string)context.Variables["fromDate"]} in the string.

    Therefore, it’s not about choosing between @{} and @() in this case, but rather understanding the context in which {} is used. If it’s inside a $"" string, it’s C# string interpolation. If it’s prefixed by @, it’s an APIM policy expression. I hope this clears up the confusion! 😊


    I hope that this response has addressed your query and helped you overcome your challenges. If so, please mark this response as Answered. This will not only acknowledge our efforts, but also assist other community members who may be looking for similar solutions.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful