diff --git a/IGeekFan.AspNetCore.Knife4jUI.sln b/IGeekFan.AspNetCore.Knife4jUI.sln index 2373bc4..5157184 100644 --- a/IGeekFan.AspNetCore.Knife4jUI.sln +++ b/IGeekFan.AspNetCore.Knife4jUI.sln @@ -26,6 +26,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WebSites", "WebSites", "{86 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OAuth2Integration", "test\WebSites\OAuth2Integration\OAuth2Integration.csproj", "{9E8D8F42-33F0-4F2D-9B56-1AB1B33DE1FA}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NSwag.Swagger.Knife4jUI", "NSwag.Swagger.Knife4jUI\NSwag.Swagger.Knife4jUI.csproj", "{32CF97CB-E877-4EB6-A9EB-03A566FFF2B0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -52,6 +54,10 @@ Global {9E8D8F42-33F0-4F2D-9B56-1AB1B33DE1FA}.Debug|Any CPU.Build.0 = Debug|Any CPU {9E8D8F42-33F0-4F2D-9B56-1AB1B33DE1FA}.Release|Any CPU.ActiveCfg = Release|Any CPU {9E8D8F42-33F0-4F2D-9B56-1AB1B33DE1FA}.Release|Any CPU.Build.0 = Release|Any CPU + {32CF97CB-E877-4EB6-A9EB-03A566FFF2B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {32CF97CB-E877-4EB6-A9EB-03A566FFF2B0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {32CF97CB-E877-4EB6-A9EB-03A566FFF2B0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {32CF97CB-E877-4EB6-A9EB-03A566FFF2B0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -63,6 +69,7 @@ Global {6C784918-BE29-4FEF-8AC3-9D34A38DE822} = {929BB2D7-C678-4BE8-8AA9-F271A2AE4545} {86851B6C-3504-4879-8464-1DB422D46BA0} = {75C51574-4CBD-403B-8182-8BF2A6DCFD43} {9E8D8F42-33F0-4F2D-9B56-1AB1B33DE1FA} = {86851B6C-3504-4879-8464-1DB422D46BA0} + {32CF97CB-E877-4EB6-A9EB-03A566FFF2B0} = {C146A419-15E0-4475-9623-706C5E2DCE0B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {9D77CCB4-F597-421B-9EF9-52D4B0AC382D} diff --git a/NSwag.Swagger.Knife4jUI/Controllers/WeatherForecastController.cs b/NSwag.Swagger.Knife4jUI/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..36ddc6e --- /dev/null +++ b/NSwag.Swagger.Knife4jUI/Controllers/WeatherForecastController.cs @@ -0,0 +1,39 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace NSwag.Swagger.Knife4jUI.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger<WeatherForecastController> _logger; + + public WeatherForecastController(ILogger<WeatherForecastController> logger) + { + _logger = logger; + } + + [HttpGet] + public IEnumerable<WeatherForecast> Get() + { + var rng = new Random(); + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = rng.Next(-20, 55), + Summary = Summaries[rng.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git a/NSwag.Swagger.Knife4jUI/NSwag.Swagger.Knife4jUI.csproj b/NSwag.Swagger.Knife4jUI/NSwag.Swagger.Knife4jUI.csproj new file mode 100644 index 0000000..c147a16 --- /dev/null +++ b/NSwag.Swagger.Knife4jUI/NSwag.Swagger.Knife4jUI.csproj @@ -0,0 +1,15 @@ +<Project Sdk="Microsoft.NET.Sdk.Web"> + + <PropertyGroup> + <TargetFramework>net5.0</TargetFramework> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="NSwag.AspNetCore" Version="13.8.2" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\src\IGeekFan.AspNetCore.Knife4jUI\IGeekFan.AspNetCore.Knife4jUI.csproj" /> + </ItemGroup> + +</Project> diff --git a/NSwag.Swagger.Knife4jUI/Program.cs b/NSwag.Swagger.Knife4jUI/Program.cs new file mode 100644 index 0000000..7894af7 --- /dev/null +++ b/NSwag.Swagger.Knife4jUI/Program.cs @@ -0,0 +1,26 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace NSwag.Swagger.Knife4jUI +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup<Startup>(); + }); + } +} diff --git a/NSwag.Swagger.Knife4jUI/Properties/launchSettings.json b/NSwag.Swagger.Knife4jUI/Properties/launchSettings.json new file mode 100644 index 0000000..8172fb9 --- /dev/null +++ b/NSwag.Swagger.Knife4jUI/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:2990", + "sslPort": 44362 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "NSwag.Swagger.Knife4jUI": { + "commandName": "Project", + "dotnetRunMessages": "true", + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/NSwag.Swagger.Knife4jUI/Startup.cs b/NSwag.Swagger.Knife4jUI/Startup.cs new file mode 100644 index 0000000..b5f8dce --- /dev/null +++ b/NSwag.Swagger.Knife4jUI/Startup.cs @@ -0,0 +1,69 @@ +using IGeekFan.AspNetCore.Knife4jUI; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.HttpsPolicy; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +// using Microsoft.OpenApi.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace NSwag.Swagger.Knife4jUI +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + + services.AddControllers(); + services.AddOpenApiDocument(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseHttpsRedirection(); + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseOpenApi(settings => + { + settings.PostProcess = (document, request) => + { + document.Info.Title = Configuration["Project:Name"]; + }; + }); + app.UseKnife4UI(c => + { + // c.RoutePrefix = "/docs"; // serve the UI at root + c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs"); + }); + // app.UseSwaggerUi3(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git a/NSwag.Swagger.Knife4jUI/WeatherForecast.cs b/NSwag.Swagger.Knife4jUI/WeatherForecast.cs new file mode 100644 index 0000000..d8cb84f --- /dev/null +++ b/NSwag.Swagger.Knife4jUI/WeatherForecast.cs @@ -0,0 +1,15 @@ +using System; + +namespace NSwag.Swagger.Knife4jUI +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git a/NSwag.Swagger.Knife4jUI/appsettings.Development.json b/NSwag.Swagger.Knife4jUI/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/NSwag.Swagger.Knife4jUI/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/NSwag.Swagger.Knife4jUI/appsettings.json b/NSwag.Swagger.Knife4jUI/appsettings.json new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ b/NSwag.Swagger.Knife4jUI/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/src/IGeekFan.AspNetCore.Knife4jUI/IGeekFan.AspNetCore.Knife4jUI.csproj b/src/IGeekFan.AspNetCore.Knife4jUI/IGeekFan.AspNetCore.Knife4jUI.csproj index 18503cc..f3b82c3 100644 --- a/src/IGeekFan.AspNetCore.Knife4jUI/IGeekFan.AspNetCore.Knife4jUI.csproj +++ b/src/IGeekFan.AspNetCore.Knife4jUI/IGeekFan.AspNetCore.Knife4jUI.csproj @@ -31,10 +31,6 @@ <ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.0' "> <FrameworkReference Include="Microsoft.AspNetCore.App" /> </ItemGroup> - <ItemGroup> - <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.5.1" /> - <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.5.1" /> - </ItemGroup> <ItemGroup> <EmbeddedResource Include="knife4j/**/*" />