Asp.Net Core API +EF+Sqllite+Automapper+搭建后端服务 系列(一)配置Swagger
gitee:NoteList: 项目使用WPF Prism MaterialDesign WebApi 基本功能 创建任务 记录任务状态 及相关统计功能 WPF (gitee.com)
1、创建项目
打开vs2019 选择ASP.Net Core web api,点击下一步,选择5.0版本 点击创建
创建完成后程序目录
2、配置Swagger
打开Nutget 包管理工具 搜索Swagger 找到Swashbuckle.AspNetCore,并安装
在Startup类中配置一下代码
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); //配置Swagger services.AddSwaggerGen(setupAction => { setupAction.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "MyApi", Version = "v1" }); }); }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); //配置Swagger app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyApi v1")); } app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
3、检查成果
F5运行,发现并没有显示Swagger
别急 修改下Uri :https://localhost:44378/swagger/index.html
配置launchSettings.json文件
{ "$schema": "http://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:52338", "sslPort": 44378 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "MyApi": { "commandName": "Project", "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "https://localhost:5001", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }