MapToApiVersion
の使用に問題があります。 http://localhost:5000/swaggerから、1.0 Docsと2.0 Docsがswaggerを正しくレンダリングし、対応するswagger.jsonファイルを使用できます。 3.0ドキュメントがレンダリングに失敗し、swagger.jsonファイルが生成されないように見えます。Microsoft.AspNetCore.Mvc.Versioning、Swashbuckle.AspNetCore、およびMaptoApiVersionの使用
実際のサービスは、3つのバージョンすべてで正しく動作しています。私が郵便配達所からそれを打つと、私は期待した反応を得る。
これは、Mvc.Versioningの1.1.1-rc1とSwashbuckle.AspNetCoreの1.0.0を実行しています。私はすべてのエラーを参照するか、私はVisual Studioのコードでデバッグでこれを実行すると例外を持っていないhttps://github.com/senften/AspNetCoreVersionedWebApi/tree/maptoapiversion
で
全コードの下に完全.csproj。私はスタートアップや宣言をめちゃくちゃにしてしまったのですか、シムやスワッシュバクルのバグに遭遇しただけですか?
using Microsoft.AspNetCore.Mvc;
using VersionedWebApi.Model;
namespace VersionedWebApi.Controllers
{
/// <summary>
/// GoodByeController, just saying Goodbye!
/// </summary>
[ApiVersion("1.0", Deprecated = true)]
[ApiVersion("3.0")]
[Route("api/v{api-version:apiVersion}/[controller]")]
public class GoodByeController : Controller
{
/// <summary>
/// Default Get call returning Goodbye world!
/// </summary>
/// <returns></returns>
[HttpGet]
[ProducesResponseType(typeof(GoodByeWorldModel), 200)]
[ProducesResponseType(typeof(void), 404)]
public GoodByeWorldModel Get()
{
return new GoodByeWorldModel();
}
/// <summary>
/// Default Get call returning Goodbye world!
/// </summary>
/// <returns></returns>
[HttpGet, MapToApiVersion("3.0")]
[ProducesResponseType(typeof(VersionedWebApi.Model.v3.GoodByeWorldModel), 200)]
[ProducesResponseType(typeof(void), 404)]
public VersionedWebApi.Model.v3.GoodByeWorldModel GetV3()
{
return new VersionedWebApi.Model.v3.GoodByeWorldModel();
}
}
}
startup.cs
using System.Xml.XPath;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SwashbuckleAspNetVersioningShim;
namespace VersionedWebApi
{
public class Startup
{
public Startup()
{
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
var mvcBuilder = services.AddMvc();
services.AddMvcCore().AddVersionedApiExplorer();
// Adds versioning capabilities, defaulting to version 1.0 calls if available
services.AddApiVersioning(o =>
{
o.AssumeDefaultVersionWhenUnspecified = true;
o.DefaultApiVersion = new ApiVersion(1, 0);
});
// Add generated documentation
services.AddSwaggerGen(c =>
{
var provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>();
SwaggerVersioner.ConfigureSwaggerVersions(c, provider);
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, ApplicationPartManager partManager, IApiVersionDescriptionProvider provider)
{
// Generate swagger.json
app.UseSwagger();
// Let's enable SwaggerUI
app.UseSwaggerUI(c =>
{
SwaggerVersioner.ConfigureSwaggerVersions(c, provider);
});
app.UseMvc();
// This is new for v1.1 and is a behavioral breaking change from previous (including 1.1-beta)
// See the release notes: https://github.com/Microsoft/aspnet-api-versioning/releases/tag/v1.1-rc1
app.UseApiVersioning();
}
}
}
csprojファイル
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<Company />
<Description>A .NET Core Web API project demonstrating versioning a Web API and generating interactive documentation with Swagger.</Description>
<RepositoryUrl>https://github.com/senften/AspNetCoreVersionedWebApi</RepositoryUrl>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile></DocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="1.1.0-rc1"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
<PackageReference Include="SwashbuckleAspNetVersioningShim" Version="1.0.0-beta4"/>
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0-msbuild3-final" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0"/>
<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="*"/>
</ItemGroup>
</Project>