An enterprise is migrating its legacy payment processing API to Azure API Management (APIM). To support blue-green deployments, the API gateway must route incoming requests dynamically based on a custom HTTP header named `X-Routing-Environment`. If the header value is `canary`, the gateway must route the request to a backend service registered with the ID `canary-endpoint`. Otherwise, the request should continue to the default backend.
Complete the following XML policy snippet to implement the dynamic routing logic. Fill in the blanks with the correct context variable and policy element name.
Cevap:Complete the XML policy configuration below to dynamically route requests based on the header value:
xml
<policies>
<inbound>
<base />
<choose>
<when condition="@(【context】.Request.Headers.GetValueOrDefault("X-Routing-Environment") == "canary")">
<【set-backend-service】 backend-id="canary-endpoint" />
</when>
</choose>
</inbound>
</policies>
xml
<policies>
<inbound>
<base />
<choose>
<when condition="@(【context】.Request.Headers.GetValueOrDefault("X-Routing-Environment") == "canary")">
<【set-backend-service】 backend-id="canary-endpoint" />
</when>
</choose>
</inbound>
</policies>
Cevap
The first blank must be filled with `context` and the second blank must be filled with `set-backend-service`.
The correct configuration uses the read-only C# context variable `context` to access the HTTP headers of the incoming request, and uses the `set-backend-service` policy to override the default backend with the registered backend service ID.
Adım Adım Çözüm
Anahtar Kavram
Dynamic backend routing using context variables and set-backend-service policies in Azure API Management.