Swagger 如何创建可导入Azure API管理门户的Web API
所以我对Azure API管理门户做了一些修改。我已经按照教程上的,并设法让它的工作 然后我创建了一个WebApi应用程序,它使用了swagger。我的配置如下:Swagger 如何创建可导入Azure API管理门户的Web API,swagger,asp.net-core-webapi,azure-api-management,Swagger,Asp.net Core Webapi,Azure Api Management,所以我对Azure API管理门户做了一些修改。我已经按照教程上的,并设法让它的工作 然后我创建了一个WebApi应用程序,它使用了swagger。我的配置如下: public void ConfigureServices(IServiceCollection services) { ... services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "My API", Ve
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
,我可以看到Swigger UI,当我单击Swigger UI上的链接或访问url时,我也可以看到规范https://my-api.azurewebsites.net/swagger/v1/swagger.json
所以我的问题是,我不知道如何将其导入AAMP。我可以将其发布到应用程序服务,并从那里开始工作,但如果我尝试导入urlhttps://my-api.azurewebsites.net/swagger/v1/swagger.json
进入AAMP后,我得到一个错误:
所以我等了一个小时,然后再试一次,结果发现相同的错误,我想我遗漏了一些东西,因为当我导入会议api规范时,它的url与我的不同,但我找不到任何东西,或者我正在搜索错误的东西。有人能在这里给我个提示吗
我也尝试过搜索会议API的源代码,这样我就可以推断出我做错了什么,但我没有找到这些源代码。通过遵循此Azure文档,将Swagger文档导入APIM非常简单。导入Swagger 1.2文档时没有问题。然而,如果您打算导入Swagger 2.0版本,您可能会面临此类问题 如果您正在使用.NETFramework4.5+构建一个API应用程序,并使用Swashback库,那么就可以了。但是,如果您正在使用ASP.NET核心构建应用程序,它确实会让您头疼。首先,查看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();
...
}
Wen需要包括两个附加属性–主机和方案。Swagger规范明确声明这两个都不是必需的。但是,APIM确实要求在swagger.json文档中包含这两个属性
那么,我们如何解决这个问题呢
对于.NET 4.5+中的应用程序,只需确保您的SwaggerConfig.cs已使用正确设置激活了这些选项:
SwaggerDocsConfig.Schemes(new[] { “http”, “https” });
SwaggerDocsConfig.RootUrl(req => GetRootUrlFromAppConfig());
在ASP.NET核心应用程序中,实现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)));
});
...
}
公共类SchemaDocumentFilter:IDocumentFilter
{
公共无效应用(SwaggerDocument swaggerDoc、DocumentFilterContext上下文)
{
swaggerDoc.Host=“localhost:44321”;
swaggerDoc.BasePath=“/”;
swaggerDoc.Schemes=newlist(){“https”};
}
}
此SchemaDocumentFilter应添加到Startup.cs中的ConfigureService方法中:
公共静态无效配置服务(此IServiceCollection服务)
{
...
services.addswagggen();
services.configureswagger文档(
选项=>
{
options.SingleApiVersion(新信息(){Version=“v1”,Title=“Swagger UI”});
options.IgnoreObsoleteActions=true;
options.OperationFilter(新的ApplyXmlActionComments(GetXmlPath(appEnv));
options.DocumentFilter();
});
services.configureswagger架构(
选项=>
{
options.descripbeAllenumsasstrings=true;
options.ignoreobsoletproperties=true;
options.customSchemaAIDS(type=>type.FriendlyId(true));
ModelFilter(新的applyxmltypeecomments(GetXmlPath(appEnv));
});
...
}
完成此操作后,将swagger.json导入APIM,然后它就可以工作了
:
希望有帮助。通过遵循此Azure文档,将Swagger文档导入APIM非常简单。导入Swagger 1.2文档时没有问题。然而,如果您打算导入Swagger 2.0版本,您可能会面临此类问题 如果您正在使用.NETFramework4.5+构建一个API应用程序,并使用Swashback库,那么就可以了。但是,如果您正在使用ASP.NET核心构建应用程序,它确实会让您头疼。首先,查看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();
...
}
Wen需要包括两个附加属性–主机和方案。Swagger规范明确声明这两个都不是必需的。但是,APIM确实要求在swagger.json文档中包含这两个属性
那么,我们如何解决这个问题呢
对于.NET 4.5+中的应用程序,只需确保您的SwaggerConfig.cs已使用正确设置激活了这些选项:
SwaggerDocsConfig.Schemes(new[] { “http”, “https” });
SwaggerDocsConfig.RootUrl(req => GetRootUrlFromAppConfig());
在ASP.NET核心应用程序中,实现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)));
});
...
}
公共类SchemaDocumentFilter:IDocumentFilter
{
公共无效应用(SwaggerDocument swaggerDoc、DocumentFilterContext上下文)
{
swaggerDoc.Host=“localhost:44321”;
swaggerDoc.BasePath=“/”;
swaggerDoc.Schemes=newlist(){“https”};
}
}
此SchemaDocumentFilter应添加到Startup.cs中的ConfigureService方法中:
公共静态无效配置服务(此IServiceCollection服务)
{
...
services.addswagggen();
services.configureswagger文档(
选项=>
{
options.SingleApiVersion(新信息(){Version=“v1”,Title=“Swagger UI”});
options.IgnoreObsoleteActions=true;
options.OperationFilter(新的ApplyXmlActionComments(GetXmlPath(appEnv));
options.DocumentFilter();
});
services.configureswagger架构(
选项=>
{
options.descripbeAllenumsasstrings=true;
options.ignoreobsoletproperties=true;
options.customSchemaAIDS(type=>type.FriendlyId(true));
ModelFilter(新的applyxmltypeecomments(GetXmlPath(appEnv));
});
...
}
一旦你赢了