因此,我对Azure API管理门户进行了一些改动。我遵循了how the import the conference api上的教程,并设法使其正常工作。
然后,我创建了一个使用招摇的WebApi应用程序。我的配置如下:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});
...
}
public void Configure(IApplicationBuilder app,
IServiceProvider services,
IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Address Service API");
});
app.UseHttpsRedirection();
app.UseMvc();
}
如果我运行此导航并导航到https://my-api/swagger
,则在单击swagger UI上的链接或访问URL https://my-api.azurewebsites.net/swagger/v1/swagger.json
时,可以看到swagger UI,也可以看到规范 >
所以我的问题是,我不知道如何将其实际导入AAMP。我可以将其发布到应用程序服务,并且可以从那里运行,但是如果我尝试将URL https://my-api.azurewebsites.net/swagger/v1/swagger.json
导入AAMP,则会出现错误:
所以我等了一个小时,然后再试一次,只是遇到相同的错误,我想我丢失了一些东西,因为当我导入会议api规范时,它的URL与我的URL不同,但是我找不到任何东西或我在寻找错误的东西。有人可以在这里给我个头吗?
我也尝试过搜索会议API的来源,这样我就可以推断出我做错了什么,但是我找不到运气。
答案 0 :(得分:0)
通过遵循此Azure文档,将Swagger文档导入APIM非常简单。导入Swagger 1.2文档没有问题。但是,如果您打算导入Swagger 2.0,则可能会遇到此类问题
如果您要使用Swashbuckle库使用.NET Framework 4.5+构建API应用,那就可以了。但是,如果您要使用ASP.NET Core来构建应用程序,那确实会让您头疼。首先,查看您的Startup.cs文件。 ConfigureService方法如下:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
services.AddSwaggerGen();
services.ConfigureSwaggerDocument(
options =>
{
options.SingleApiVersion(new Info() { Version = "v1", Title = "Swagger UI" });
options.IgnoreObsoleteActions = true;
options.OperationFilter(new ApplyXmlActionComments(GetXmlPath(appEnv)));
});
services.ConfigureSwaggerSchema(
options =>
{
options.DescribeAllEnumsAsStrings = true;
options.IgnoreObsoleteProperties = true;
options.CustomSchemaIds(type => type.FriendlyId(true));
options.ModelFilter(new ApplyXmlTypeComments(GetXmlPath(appEnv)));
});
...
}
private static string GetXmlPath(IApplicationEnvironment appEnv)
{
var assembly = typeof(Startup).GetTypeInfo().Assembly;
var assemblyName = assembly.GetName().Name;
var path = $@"{appEnv.ApplicationBasePath}\{assemblyName}.xml";
if (File.Exists(path))
{
return path;
}
var config = appEnv.Configuration;
var runtime = $"{appEnv.RuntimeFramework.Identifier.ToLower()}{appEnv.RuntimeFramework.Version.ToString().Replace(".", string.Empty)}";
path = $@"{appEnv.ApplicationBasePath}\..\..\artifacts\bin\{assemblyName}\{config}\{runtime}\{assemblyName}.xml";
return path;
}
除此之外,Configure方法可能类似于:
public void Configure(IApplicationBuilder app)
{
...
app.UseSwaggerGen();
app.UseSwaggerUi();
...
}
我们需要包括两个附加属性-主机和方案。 Swagger规范明确声明两者都不是必需的。但是,APIM DOES要求将两个属性都包含在swagger.json文档中。
那么,我们如何解决呢?
对于.NET 4.5+中的应用程序,只需确保您的SwaggerConfig.cs已使用正确的设置激活了这些选项:
SwaggerDocsConfig.Schemes(new[] { “http”, “https” });
SwaggerDocsConfig.RootUrl(req => GetRootUrlFromAppConfig());
在ASP.NET Core应用程序中,由于您应该实现IDocumentFilter接口,因此可能会比较棘手。这是示例代码:
public class SchemaDocumentFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
swaggerDoc.Host = "localhost:44321";
swaggerDoc.BasePath = "/";
swaggerDoc.Schemes = new List<string>() { "https" };
}
}
And this SchemaDocumentFilter should be added into your ConfigureService method in Startup.cs:
public static void ConfigureServices(this IServiceCollection services)
{
...
services.AddSwaggerGen();
services.ConfigureSwaggerDocument(
options =>
{
options.SingleApiVersion(new Info() { Version = "v1", Title = "Swagger UI" });
options.IgnoreObsoleteActions = true;
options.OperationFilter(new ApplyXmlActionComments(GetXmlPath(appEnv)));
options.DocumentFilter<SchemaDocumentFilter>();
});
services.ConfigureSwaggerSchema(
options =>
{
options.DescribeAllEnumsAsStrings = true;
options.IgnoreObsoleteProperties = true;
options.CustomSchemaIds(type => type.FriendlyId(true));
options.ModelFilter(new ApplyXmlTypeComments(GetXmlPath(appEnv)));
});
...
}
完成此操作后,将您的swagger.json导入APIM,即可正常工作。
希望有帮助。