C# ASP.Core Web API的swagger环境配置
发布日期:2021-05-04 14:31:25 浏览次数:30 分类:原创文章

本文共 2796 字,大约阅读时间需要 9 分钟。

本文代码仓库地址:


一、新建项目

【文件】 – 【新建项目】 – 【ASP.Core Web】 – 【API】
在这里插入图片描述
在这里插入图片描述

二、安装 swagger

【工具】-- 【NuGet包管理器】–【程序包管理器控制台】-- 然后在控制台输入:
Install-Package Swashbuckle.AspNetCore -Version 5.0.0-rc4
在这里插入图片描述
在这里插入图片描述

三、Startup.cs 文件中引用服务

using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Hosting;using Microsoft.Extensions.Logging;using Microsoft.OpenApi.Models;namespace WebApplication1 {     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)    {         // 1.安装:Install-Package Swashbuckle.AspNetCore -Version 5.0.0-rc4      // 2-1.注册服务      services.AddSwaggerGen(m =>      {           m.SwaggerDoc("v1", new OpenApiInfo {    Title = "WebApplication1", Version = "v1"});      });      services.AddControllers();    }    // 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.UseRouting();      app.UseAuthorization();      // 2-2.引用服务      app.UseSwagger();      app.UseSwaggerUI(m => {           m.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication1");        }      );      // 3.启动【地址:http://localhost:58464/swagger/index.html】      // 这里到这里环境就已经配置好了,有需要可以继续下面的      // 4-1.修改默认启动页【Properties 下的 launchSettings.json】      app.UseEndpoints(endpoints =>      {           endpoints.MapControllers();      });    }  }}

四、修改默认启动页

{     "$schema": "http://json.schemastore.org/launchsettings.json",  "iisSettings": {       "windowsAuthentication": false,    "anonymousAuthentication": true,    "iisExpress": {         "applicationUrl": "http://localhost:58464",      "sslPort": 0    }  },  "profiles": {       "IIS Express": {         "commandName": "IISExpress",      "launchBrowser": true,      // "launchUrl": "weatherforecast",      // 4-2.在这里修改默认启动页      "launchUrl": "swagger/index.html",      "environmentVariables": {           "ASPNETCORE_ENVIRONMENT": "Development"      }    },    "WebApplication1": {         "commandName": "Project",      "launchBrowser": true,      "launchUrl": "weatherforecast",      "applicationUrl": "http://localhost:5000",      "environmentVariables": {           "ASPNETCORE_ENVIRONMENT": "Development"      }    }  }}

一点点笔记,以便以后翻阅。

上一篇:Delphi 线程的开始、暂停、继续、停止
下一篇:Vue 用户信息表的添加删除

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月05日 07时45分36秒