在Azure API管理器中管理版本化的API [英] Manage versioned API in Azure API Manager

本文介绍了在Azure API管理器中管理版本化的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用API​​应用程序在Azure中托管我们的Web API.我在API应用程序的前面使用Azure API管理器将开发人员门户暴露给我们的某些消费者. Web API内置于.NET Core中,并且使用URL( https://example.com/api/v2/controller .. .).我已经给予了swagger支持,并且为每个版本创建了一个swagger.json.我在ARM模板中使用了这些庞大的文件来设置API管理器并公开此版本化的API.将版本添加到API Manager时,我必须提供一个路径,该路径对于API Im添加必须是唯一的.因此,对于版本v1,我给出"api/v1",对于版本v2,我给出"api/v2".到目前为止,一切都很好.问题是当尝试使用API​​ Manager调用API时,URL最终变为: https://foo-api-manager-dev.azure-api.net/api/v2/api/v2/tests 因此,我复制了"api/v2"部分.我想这与API Manager的工作方式以及Web API中的版本控制有关.直接将控制器调用到API App时,URL是正确的.

I´m looking into host our web API in Azure using an API app. I am using the Azure API Manager in front of the API App to expose the developer portal to some of our consumers. The web API is built in .NET core and it has version support using the URL (https://example.com/api/v2/controller...). I have given it swagger support and one swagger.json is created for each version. These swagger files I use in my ARM templates to set up API Manager and expose this versioned API. When adding a version to API Manager I have to give a path which must be unique for the API Im adding. So for version v1 I give "api/v1" and for v2 I give "api/v2". So far so good. The problem is when trying to call the API using API Manager the URL ends up to be: https://foo-api-manager-dev.azure-api.net/api/v2/api/v2/tests So the "api/v2"-part i duplicated. I guess it has something to do with how API Manager works and how my versioning is set up in my web API. When calling the controllers directly to the API App then the URL is correct.

我进行了很多搜索,并尝试了不同的方法,但是这里确实需要一些输入.感谢所有建议!

I have searched around a lot and tried different approaches to this but could really need some input here. All suggestions are appreciated!

我的路由是在基本控制器中管理的,看起来像这样

My routing is managed in a base controller and looks like this

[Produces("application/json")]
[Route("api/v{version:apiVersion}/[controller]")]
public abstract class VersionBaseController : ApiController
{
    protected VersionBaseController()
       : base()
    {
    }
}

像这样的控制器:

[ApiVersion("2.0")]
public class TestsController : VersionBaseController
{
    [HttpGet]
    public string Get()
    {
        return "Get works V2";
    }

用于创建api管理器的整个模板:

The entire template to create the api manager:

//Create API Manager
{
  "apiVersion": "2018-06-01-preview",
  "name": "[parameters('apim_name')]",
  "location": "[parameters('location')]",
  "type": "Microsoft.ApiManagement/service",
  "sku": {
    "name": "[parameters('apim_tier')]",
    "capacity": 1
  },
  "properties": {
    "publisherEmail": "[parameters('apim_adminEmail')]",
    "publisherName": "[parameters('apim_orgName')]"
  },
  "resources": [
    //Create version set
    {
      "apiVersion": "2017-03-01",
      "type": "Microsoft.ApiManagement/service/api-version-sets",
      "name": "[concat(parameters('apim_name'), '/', parameters('VersionSetName'))]",
      "dependsOn": [
        "[concat('Microsoft.ApiManagement/service/', parameters('apim_name'))]"
      ],
      "properties": {
        "description": "Version configuration",
        "displayName": "Api set 1",
        "versioningScheme": "Segment"
      }
    },
    //Link version 1
    {
      "apiVersion": "2017-03-01",
      "type": "apis",
      "name": "AvidaAPIV1",
      "dependsOn": [
        "[resourceId('Microsoft.ApiManagement/service/api-version-sets', parameters('apim_name'), parameters('VersionSetName'))]",
        "[concat('Microsoft.ApiManagement/service/', parameters('apim_name'))]"
      ],
      "properties": {
        "contentFormat": "swagger-link-json",
        "contentValue": "[concat(parameters('ExternalApiBaseUrl'),'/swagger/v1/swagger.json')]",
        "path": "/api/v1",
        "isCurrent": true,
        "apiVersion": "v1",
        "apiVersionName": "v1",
        "apiVersionDescription": "string",
        "apiVersionSetId": "[concat('Microsoft.ApiManagement/service/api-version-sets', parameters('VersionSetName'))]"
      }
    },
    //Link version 2
    {
      "apiVersion": "2017-03-01",
      "type": "apis",
      "name": "AvidaAPIV2",
      "dependsOn": [
        "[resourceId('Microsoft.ApiManagement/service/api-version-sets', parameters('apim_name'), parameters('VersionSetName'))]",
        "[concat('Microsoft.ApiManagement/service/', parameters('apim_name'))]"
      ],
      "properties": {
        "contentFormat": "swagger-link-json",
        "contentValue": "[concat(parameters('ExternalApiBaseUrl'),'/swagger/v2/swagger.json')]",
        "path": "/api/v2",
        "isCurrent": false,
        "apiVersion": "v2",
        "apiVersionName": "v2",
        "apiVersionDescription": "string",
        "apiVersionSetId": "[concat('Microsoft.ApiManagement/service/api-version-sets', parameters('VersionSetName'))]"
      }
    },
    //Create unlimited product
    {
      "apiVersion": "2017-03-01",
      "type": "products",
      "name": "[concat('UnlimitedProduct', parameters('ProductNameSuffix'))]",
      "dependsOn": [
        "[concat('Microsoft.ApiManagement/service/', parameters('apim_name'))]"
      ],
      "properties": {
        "displayName": "[concat('UnlimitedProduct', parameters('ProductNameSuffix'))]",
        "description": "Unlimited external access",
        "terms": "",
        "subscriptionRequired": false,
        "state": "published"
      },
      "resources": [
        {
          "apiVersion": "2017-03-01",
          "type": "apis",
          "name": "AvidaAPIV1",
          "dependsOn": [
            "[concat('Microsoft.ApiManagement/service/', parameters('apim_name'))]",
            "[concat('Microsoft.ApiManagement/service/', parameters('apim_name'), '/apis/AvidaAPIV1')]",
            //"[concat('Microsoft.ApiManagement/service/', parameters('apim_name'), '/apis/AvidaAPIV2')]",
            "[concat('Microsoft.ApiManagement/service/', parameters('apim_name'), '/products/UnlimitedProduct', parameters('ProductNameSuffix'))]"
          ]
        }
      ]
    }

推荐答案

所以我意识到在APIM中根本没有创建任何版本集: 我上面的模板脚本生成的API

So I realized in APIM there is no version set created at all: The resulting APIs from my above template scripts

然后我手动创建了版本集,并在Azure门户中将其链接到我的2张招贴,然后看起来是正确的: 在门户网站中手动创建

I then created version set manually and linked to my 2 swaggers in the Azure portal and then it looks correct: Manually created in portal

但是,即使手动创建版本,URL也存在相同的问题.我需要复制"api/v2/api/v2/[controller]"

But even when creating the version manually I have the same problem with the URL. I need to duplicate "api/v2/api/v2/[controller]"

现在版本2的设置对我来说似乎是正确的: 在此处输入图片描述

The settings for version2 now looks like this which seems correct to me: enter image description here

这篇关于在Azure API管理器中管理版本化的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆