diff --git a/IGeekFan.AspNetCore.Knife4jUI.sln b/IGeekFan.AspNetCore.Knife4jUI.sln index fa97e6b..35f840d 100644 --- a/IGeekFan.AspNetCore.Knife4jUI.sln +++ b/IGeekFan.AspNetCore.Knife4jUI.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30330.147 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.31903.286 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{929BB2D7-C678-4BE8-8AA9-F271A2AE4545}" EndProject @@ -9,11 +9,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{75C51574-4 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Basic", "test\Basic\Basic.csproj", "{88972F96-96D7-40A7-90DA-ED04C9ED9B33}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{98C9AB6E-7179-4594-9703-BB0BB6D7949C}" - ProjectSection(SolutionItems) = preProject - README.md = README.md - EndProjectSection -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Knife4jUIDemo", "test\Knife4jUIDemo\Knife4jUIDemo.csproj", "{86A24FA0-E85D-4BDD-97D7-B990C50A40A9}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{C146A419-15E0-4475-9623-706C5E2DCE0B}" @@ -28,6 +23,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OAuth2Integration", "test\W EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NSwag.Swagger.Knife4jUI", "test\WebSites\NSwag.Swagger.Knife4jUI\NSwag.Swagger.Knife4jUI.csproj", "{42B4C1C3-AE38-47C7-AAAA-FE0FDA7DADEB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCore6Api", "samples\AspNetCore6Api\AspNetCore6Api.csproj", "{43B995C5-145B-4D46-B4C3-6340F31A1572}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A08844CF-E477-4104-98CB-EB6417CB1B0C}" + ProjectSection(SolutionItems) = preProject + .gitattributes = .gitattributes + .gitignore = .gitignore + LICENSE = LICENSE + README.md = README.md + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -58,6 +63,10 @@ Global {42B4C1C3-AE38-47C7-AAAA-FE0FDA7DADEB}.Debug|Any CPU.Build.0 = Debug|Any CPU {42B4C1C3-AE38-47C7-AAAA-FE0FDA7DADEB}.Release|Any CPU.ActiveCfg = Release|Any CPU {42B4C1C3-AE38-47C7-AAAA-FE0FDA7DADEB}.Release|Any CPU.Build.0 = Release|Any CPU + {43B995C5-145B-4D46-B4C3-6340F31A1572}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {43B995C5-145B-4D46-B4C3-6340F31A1572}.Debug|Any CPU.Build.0 = Debug|Any CPU + {43B995C5-145B-4D46-B4C3-6340F31A1572}.Release|Any CPU.ActiveCfg = Release|Any CPU + {43B995C5-145B-4D46-B4C3-6340F31A1572}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -70,6 +79,7 @@ Global {86851B6C-3504-4879-8464-1DB422D46BA0} = {75C51574-4CBD-403B-8182-8BF2A6DCFD43} {9E8D8F42-33F0-4F2D-9B56-1AB1B33DE1FA} = {86851B6C-3504-4879-8464-1DB422D46BA0} {42B4C1C3-AE38-47C7-AAAA-FE0FDA7DADEB} = {86851B6C-3504-4879-8464-1DB422D46BA0} + {43B995C5-145B-4D46-B4C3-6340F31A1572} = {C146A419-15E0-4475-9623-706C5E2DCE0B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {9D77CCB4-F597-421B-9EF9-52D4B0AC382D} diff --git a/samples/AspNetCore6Api/AspNetCore6Api.csproj b/samples/AspNetCore6Api/AspNetCore6Api.csproj new file mode 100644 index 0000000..6f55844 --- /dev/null +++ b/samples/AspNetCore6Api/AspNetCore6Api.csproj @@ -0,0 +1,20 @@ + + + + net6.0 + enable + enable + + + AspNetCore6Api.xml + + + + + + + + + + + diff --git a/samples/AspNetCore6Api/AspNetCore6Api.xml b/samples/AspNetCore6Api/AspNetCore6Api.xml new file mode 100644 index 0000000..0b836c0 --- /dev/null +++ b/samples/AspNetCore6Api/AspNetCore6Api.xml @@ -0,0 +1,121 @@ + + + + AspNetCore6Api + + + + + 中文这是一个Get请求这是一个Get请求 + + + + + 得到一个ErrorCode + + + + + + 发送一个Post + + + + + + 这是一个Get请求 + + + + + + 请求实体 + + + + + 异常信息 + + + + + 状态码 + + + + + 注释ErrorCode + + + + + 操作成功 + + + + + 未知错误 + + + + + 服务器未知错误 + + + + + 失败 + + + + + 认证失败 + + + + + 无权限 + + + + + 失败 + + + + + refreshToken异常 + + + + + 资源不存在 + + + + + 参数错误 + + + + + 令牌失效 + + + + + 令牌过期 + + + + + 字段重复 + + + + + 禁止操作 + + + + diff --git a/samples/AspNetCore6Api/Controllers/WeatherForecastController.cs b/samples/AspNetCore6Api/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..ac001b6 --- /dev/null +++ b/samples/AspNetCore6Api/Controllers/WeatherForecastController.cs @@ -0,0 +1,95 @@ +using Knife4jUIDemo; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace AspNetCore6Api.Controllers +{ + /// + /// һGetһGet + /// + [ApiController] + [Route("api/WeatherForecast/[action]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + /// + /// õһErrorCode + /// + /// + [HttpGet] + public ErrorCode GetErrorCode() + { + return ErrorCode.Success; + } + + [HttpGet] + public ErrorCode GetErrorCode2(ErrorCode errorCode) + { + return errorCode; + } + + [HttpGet] + public IActionResult GetErrorCode4(ErrorCode errorCode) + { + return new JsonResult(new PostErrorCodeDto() { Message = "a", ErrorCode = errorCode }); + } + + /// + /// һPost + /// + /// + [HttpPost] + public PostErrorCodeDto PostErrorCode([FromBody] PostErrorCodeDto PostErrorCodeDto) + { + return PostErrorCodeDto; + } + + /// + /// һGet + /// + /// + [HttpGet] + public IEnumerable 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(); + } + } + + /// + /// ʵ + /// + public class PostErrorCodeDto + { + /// + /// 쳣Ϣ + /// + public string Message { get; set; } + + public WeatherForecast WeatherForecast { get; set; } + /// + /// ״̬ + /// + public ErrorCode ErrorCode { get; set; } + } +} diff --git a/samples/AspNetCore6Api/ErrorCode.cs b/samples/AspNetCore6Api/ErrorCode.cs new file mode 100644 index 0000000..05dfd2f --- /dev/null +++ b/samples/AspNetCore6Api/ErrorCode.cs @@ -0,0 +1,81 @@ +using System.ComponentModel; + +namespace Knife4jUIDemo +{ + /// + /// 注释ErrorCode + /// + public enum ErrorCode + { + /// + /// 操作成功 + /// + Success = 0, + /// + /// 未知错误 + /// + UnknownError = 1007, + /// + /// 服务器未知错误 + /// + ServerUnknownError = 999, + + /// + /// 失败 + /// + Error = 1000, + + /// + /// 认证失败 + /// + AuthenticationFailed = 10000, + /// + /// 无权限 + /// + NoPermission = 10001, + /// + /// 失败 + /// + Fail = 9999, + /// + /// refreshToken异常 + /// + RefreshTokenError = 10100, + /// + /// 资源不存在 + /// + NotFound = 10020, + /// + /// 参数错误 + /// + [Description("参数错误")] + ParameterError = 10030, + /// + /// 令牌失效 + /// + [Description("令牌失效")] + TokenInvalidation = 10040, + /// + /// 令牌过期 + /// + TokenExpired = 10050, + /// + /// 字段重复 + /// + RepeatField = 10060, + /// + /// 禁止操作 + /// + Inoperable = 10070, + //10080 请求方法不允许 + + //10110 文件体积过大 + + //10120 文件数量过多 + + //10130 文件扩展名不符合规范 + + //10140 请求过于频繁,请稍后重试 + ManyRequests = 10140 + } +} diff --git a/samples/AspNetCore6Api/Program.cs b/samples/AspNetCore6Api/Program.cs new file mode 100644 index 0000000..265788d --- /dev/null +++ b/samples/AspNetCore6Api/Program.cs @@ -0,0 +1,44 @@ +using IGeekFan.AspNetCore.Knife4jUI; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.OpenApi.Models; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(c => +{ + c.SwaggerDoc("v1", new OpenApiInfo { Title = "API V1", Version = "v1" }); + var filePath = Path.Combine(System.AppContext.BaseDirectory, "AspNetCore6Api.xml"); + c.IncludeXmlComments(filePath, true); +}); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseKnife4UI(c => + { + c.RoutePrefix = "k4"; // serve the UI at root + c.SwaggerEndpoint("/v1/swagger.json", "V1 Docs"); + }); + +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.UseRouting(); +app.UseEndpoints(endpoints => +{ + endpoints.MapControllers(); + endpoints.MapSwagger("/k4/{documentName}/swagger.json"); +}); + +app.Run(); diff --git a/samples/AspNetCore6Api/Properties/launchSettings.json b/samples/AspNetCore6Api/Properties/launchSettings.json new file mode 100644 index 0000000..59a6a29 --- /dev/null +++ b/samples/AspNetCore6Api/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:7480", + "sslPort": 44309 + } + }, + "profiles": { + "AspNetCore6Api": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7030;http://localhost:5030", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/samples/AspNetCore6Api/WeatherForecast.cs b/samples/AspNetCore6Api/WeatherForecast.cs new file mode 100644 index 0000000..8370e19 --- /dev/null +++ b/samples/AspNetCore6Api/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace AspNetCore6Api +{ + 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; } + } +} \ No newline at end of file diff --git a/samples/AspNetCore6Api/appsettings.Development.json b/samples/AspNetCore6Api/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/samples/AspNetCore6Api/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/samples/AspNetCore6Api/appsettings.json b/samples/AspNetCore6Api/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/samples/AspNetCore6Api/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/test/Basic/Basic.csproj b/test/Basic/Basic.csproj index 8b268e5..d7aa9b4 100644 --- a/test/Basic/Basic.csproj +++ b/test/Basic/Basic.csproj @@ -7,6 +7,7 @@ + @@ -16,6 +17,6 @@ - + \ No newline at end of file diff --git a/test/Basic/Startup.cs b/test/Basic/Startup.cs index 881acfb..9174f64 100644 --- a/test/Basic/Startup.cs +++ b/test/Basic/Startup.cs @@ -55,7 +55,7 @@ namespace Basic //c.EnableAnnotations(); c.AddServer(new OpenApiServer() { - Url = "", + Url = "http://localhost:81/admin/", Description = "vvv" }); c.CustomOperationIds(apiDesc => diff --git a/test/Knife4jUIDemo/Knife4jUIDemo.csproj b/test/Knife4jUIDemo/Knife4jUIDemo.csproj index 31e7307..4d3bf27 100644 --- a/test/Knife4jUIDemo/Knife4jUIDemo.csproj +++ b/test/Knife4jUIDemo/Knife4jUIDemo.csproj @@ -12,7 +12,7 @@ - + diff --git a/test/Knife4jUIDemo/Startup.cs b/test/Knife4jUIDemo/Startup.cs index 73cdae1..7f1d5dc 100644 --- a/test/Knife4jUIDemo/Startup.cs +++ b/test/Knife4jUIDemo/Startup.cs @@ -23,14 +23,19 @@ namespace Knife4jUIDemo public void ConfigureServices(IServiceCollection services) { services.AddControllers(); + services.Configure(c => + { + Configuration.Bind("Knife4UI", c); + }); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "API V1", Version = "v1" }); c.AddServer(new OpenApiServer() { - //Url = "http://localhost:81/admin/", + Url = "http://localhost:81/admin/", Description = "vvv" }); + c.CustomOperationIds(apiDesc => { var controllerAction = apiDesc.ActionDescriptor as ControllerActionDescriptor; diff --git a/test/Knife4jUIDemo/appsettings.json b/test/Knife4jUIDemo/appsettings.json index d9d9a9b..85610e4 100644 --- a/test/Knife4jUIDemo/appsettings.json +++ b/test/Knife4jUIDemo/appsettings.json @@ -6,5 +6,9 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "Knife4UI": { + "DocumentTitle": "Knife4jUI Demo" + + } }