using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.SwaggerGen; using System.Text; namespace Blahblah.FlowerStory.Server.Controller; /// [Route("apidoc")] public class SwaggerController : ControllerBase { private readonly SwaggerGenerator generator; /// public SwaggerController(SwaggerGenerator generator) { this.generator = generator; } /// [Route("get/{version}")] [HttpGet] public ActionResult GetApi(string version) { var model = generator.GetSwagger(version); var builder = new StringBuilder(); builder.Append($@" Flower Story - API 接口文档

{model.Info.Title}

接口文档 {model.Info.Version}

{model.Info.Description}

"); foreach (var item in model.Paths) { if (item.Value.Operations != null) { foreach (var operation in item.Value.Operations) { if (string.IsNullOrEmpty(operation.Value.Summary)) { continue; } builder.Append($@"

{operation.Value.Summary}

"); if (operation.Value.Parameters?.Count > 0) { builder.Append(@" "); foreach (var param in operation.Value.Parameters) { builder.Append($@" "); } } if (operation.Value.Responses?.Count > 0) { builder.Append(@" "); foreach (var response in operation.Value.Responses) { builder.Append($@" "); } } builder.Append(@"
URL {item.Key}
请求方式 {operation.Key}
参数 参数类型 是否必须 说明
{param.Name} {param.In} {param.Required} {param.Description}
状态码 说明
{response.Key} {response.Value.Description}
"); } } } builder.Append(@"
"); return Content(builder.ToString(), "text/html"); } }