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.