如何通过apiVersionSetId创建API操作时更新父API集的serviceUrl
版本化API操作的ServiceUrl更新方法
要将API操作的serviceUrl关联到刚创建的版本化API,可按以下两种场景处理:
1. 新建操作时直接配置版本化地址
如果是刚创建API操作,直接在BICEP定义中指定版本化的后端地址,有两种配置方式:
方式一:继承API的版本化ServiceUrl
先确保API本身的serviceUrl已更新为版本化地址(比如${nameSearchUrl}/v1),操作会自动继承该地址,只需定义相对路径的urlTemplate:
// 先定义版本集 resource apiVersionSet 'Microsoft.ApiManagement/service/apiVersionSets@2022-08-01' = { parent: apimService name: 'names-api-version-set' properties: { displayName: 'Names APIs Version Set' versioningScheme: 'Segment' // 版本策略:路径段/查询参数/请求头,按需选择 apiVersion: 'v1' } } // 关联版本集并更新API的serviceUrl为版本化地址 resource NamesApi 'Microsoft.ApiManagement/service/apis@2022-08-01' = { name: '${name}-api-transaction' parent: apimService properties: { displayName: 'Names APIs' apiRevision: '1' subscriptionRequired: true serviceUrl: '${nameSearchUrl}/v1' // 这里设置为版本化的后端根地址 path: 'internal/name' protocols: [ 'http' 'https' ] apiVersionSetId: apiVersionSet.id apiVersion: 'v1' authenticationSettings: {} subscriptionKeyParameterNames: { header: 'Ocp-Apim-Subscription-Key' query: 'subscription-key' } isCurrent: true } } // 新建操作,继承API的版本化serviceUrl resource getNameOperation 'Microsoft.ApiManagement/service/apis/operations@2022-08-01' = { parent: NamesApi name: 'get-name' properties: { displayName: 'Get Name' method: 'GET' urlTemplate: '/search' // 最终后端地址:${nameSearchUrl}/v1/search } }
方式二:为操作单独指定版本化ServiceUrl
如果操作需要独立的版本化地址,直接在操作的properties中覆盖serviceUrl:
resource getNameOperation 'Microsoft.ApiManagement/service/apis/operations@2022-08-01' = { parent: NamesApi name: 'get-name' properties: { displayName: 'Get Name' method: 'GET' urlTemplate: '/search' serviceUrl: '${nameSearchUrl}/v1/search' // 直接指定版本化的完整后端地址 } }
2. 更新已存在的API操作
如果操作已经创建,只需在BICEP中重新定义该操作资源,修改serviceUrl或urlTemplate即可触发更新:
resource existingGetNameOperation 'Microsoft.ApiManagement/service/apis/operations@2022-08-01' = { parent: NamesApi name: 'get-name' // 必须与现有操作名称一致 properties: { displayName: 'Get Name' method: 'GET' urlTemplate: '/search' serviceUrl: '${nameSearchUrl}/v1/search' // 更新为版本化地址 } }
注意事项
- 版本策略(
versioningScheme)决定了版本的传递方式(路径段、查询参数、请求头),需确保后端服务的地址格式与版本策略匹配。 - 如果使用路径段模式(
Segment),API的path会自动包含版本号,此时后端地址也需对应包含版本路径。
内容的提问来源于stack exchange,提问作者William Fernandes
相关产品推荐
相关产品推荐

