《Asp.Net Core3 + Vue3入坑教程》-Net Core项目搭建与Swagger配置步骤

《Asp.Net Core3 + Vue3入坑教程》 此教程仅适合新手入门或者前后端分离尝试者。可以根据图文一步一步进操作编码也可以选择直接查看源码。每一篇文章都有对应的源码

教程后期会将 .Net Core 3升级成 .Net Core 5

目录 《Asp.Net Core3 + Vue3入坑教程》系列教程目录

Asp.Net Core后端项目

后端项目搭建与Swagger配置步骤

(暂未发表敬请期待...)CORS跨域问题处理

(暂未发表敬请期待...)AutoMapper & Restful API

(暂未发表敬请期待...)EF Core & Postgresql

(暂未发表敬请期待...).Net Core 3升级成 .Net Core 5

(暂未发表敬请期待...)JWT

Vue3 前端项目

暂未发表敬请期待...

本文简介

本文为《Asp.Net Core3 + Vue3入坑教程》系列教程的后端开篇,主要介绍 Asp.Net Core Web后端项目的搭建流程与Swagger配置。

Simple项目搭建流程与Swagger配置步骤 新建项目

《Asp.Net Core3 + Vue3入坑教程》-Net Core项目搭建与Swagger配置步骤


《Asp.Net Core3 + Vue3入坑教程》-Net Core项目搭建与Swagger配置步骤


《Asp.Net Core3 + Vue3入坑教程》-Net Core项目搭建与Swagger配置步骤

引入Swagger Nuget包

《Asp.Net Core3 + Vue3入坑教程》-Net Core项目搭建与Swagger配置步骤


《Asp.Net Core3 + Vue3入坑教程》-Net Core项目搭建与Swagger配置步骤

配置Starup.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.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Simple_Asp.Net_Core.ServiceProvider; namespace Simple_Asp.Net_Core { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddSwagger(); } // 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.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiHelp V1"); }); } app.UseRouting(); app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute()); } } } 配置XML 文档文件

目的是让项目的注释能够展示在swagger页面上 。XML 文档文件的路径需要与下一步Swagger扩展类的文件路径一致

var xmlPath = Path.Combine(basePath, "Simple_Asp.Net_Core.xml");

《Asp.Net Core3 + Vue3入坑教程》-Net Core项目搭建与Swagger配置步骤


《Asp.Net Core3 + Vue3入坑教程》-Net Core项目搭建与Swagger配置步骤

新建文件夹ServiceProvider,增加Swagger扩展类

当前Swagger扩展类,包含了很多内容,后续会陆续使用上

代码如下:

using System; using System.IO; using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; namespace Simple_Asp.Net_Core.ServiceProvider { public static class Swagger { public static void AddSwagger(this IServiceCollection services) { services.AddSwaggerGen(option => { option.SwaggerDoc("v1", new OpenApiInfo { Version = "0.0.1", Title = "Simple API", Description = "框架说明文档", TermsOfService = null, Contact = new OpenApiContact { Name = "Simple", Email = string.Empty, Url = null } }); // 读取xml信息 var basePath = AppContext.BaseDirectory; var xmlPath = Path.Combine(basePath, "Simple_Asp.Net_Core.xml"); option.IncludeXmlComments(xmlPath, true); // Add security definitions option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme() { Description = "Please enter into field the word 'Bearer' followed by a space and the JWT value", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, }); option.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference() { Id = "Bearer", Type = ReferenceType.SecurityScheme } }, Array.Empty<string>() } }); }); } } } 修改launchSettings.json

目的是让项目启动页为Swagger页面

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wspysg.html