
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" } } }}
一点点笔记,以便以后翻阅。
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月05日 07时45分36秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
SSLOJ1210最佳游览问题
2019-03-04
P1026 统计单词个数&&SSL1017
2019-03-04
SSLOJ1692 USACO 3.2 Magic Squares 魔板&P2730
2019-03-04
SSLOJ1063 统计数字
2019-03-04
P4305 [JLOI2011]不重复数字
2019-03-04
P3957 [NOIP2017 普及组] 跳房子
2019-03-04
P3374 【模板】树状数组 1
2019-03-04
《ybtoj高效进阶》第二部分第二章例题5 子正方形
2019-03-04
P1381 单词背诵
2019-03-04
SSLOJ1230 战略游戏
2019-03-04
P5854 【模板】笛卡尔树
2019-03-04
SpringMVC的基础配置之注解驱动
2019-03-04
在Ubuntu上安装GCC编译器
2019-03-04
Maven(高级)之聚合
2019-03-04
快速构建SpringBoot工程
2019-03-04
SpringBoot配置之配置文件分类
2019-03-04
Vue中使用v-for不能用index作为key值
2019-03-04
position: fixed如何相对父元素定位
2019-03-04
SecureCRT注册机
2019-03-04
供应商解决了mini-LED的生产问题 新款MBP蓄势待发?
2019-03-04