Pingpong
Pingpong

Reputation: 8009

Get Base URL and that with version identifier via Azure APIM Policy

Is it possible to, within policy, get the base url and that with version hightlighted below: enter image description here A url is needed like below:

@(base url with version identifier)

Above is used in find-and-replace element below:

  <policies>
        <inbound>
            <base />
            <set-backend-service base-url="https://my.com/oidc" />        
        </inbound>
        <backend>
            <base />
        </backend>
        <outbound>
            <base />
            <find-and-replace from="https://thirdparty/certs" 
                 to="@(base url with version identifier)/certs" />        
        </outbound>
        <on-error>
            <base />
        </on-error>
    </policies>

Update

How can I get the base url too? The reason is that sometimes, the versioned url is not specified, in this case, only the base url is used to repalce.

https://learn.microsoft.com/en-us/azure/api-management/api-management-policy-expressions#ref-iurl

Upvotes: 0

Views: 2975

Answers (1)

Silly John
Silly John

Reputation: 1704

You can get the base url with version using the below code snippet

@{
     var methodRoute = context.Request.Url.ToString().Replace(context.Api.ServiceUrl.ToString(),"");
     var frontEndServiceUrl = context.Request.OriginalUrl.ToString().Replace(methodRoute,"");
}

Upvotes: 1

Related Questions