《Asp.Net Core3 + Vue3入坑教程》-Net Core專案搭建與Swagger配置步驟
阿新 • • 發佈:2021-02-27
# 簡介
《Asp.Net Core3 + Vue3入坑教程》 此教程僅適合新手入門或者前後端分離嘗試者。可以根據圖文一步一步進操作編碼也可以選擇直接檢視原始碼。每一篇文章都有對應的原始碼
> 教程後期會將 .Net Core 3升級成 .Net Core 5
# 目錄
#### 《Asp.Net Core3 + Vue3入坑教程》系列教程目錄
Asp.Net Core後端專案
1. [後端專案搭建與Swagger配置步驟](https://www.cnblogs.com/Iannnnnnnnnnnnn/p/14435589.html)
2. (暫未發表敬請期待...)[CORS跨域問題處理](https://www.cnblogs.com/Iannnnnnnnnnnnn/p/14438539.html)
3. (暫未發表敬請期待...)[AutoMapper & Restful API](https://www.cnblogs.com/Iannnnnnnnnnnnn/p/14438949.html)
4. (暫未發表敬請期待...)[EF Core & Postgresql](https://www.cnblogs.com/Iannnnnnnnnnnnn/p/14438885.html)
5. (暫未發表敬請期待...)[.Net Core 3升級成 .Net Core 5](https://www.cnblogs.com/Iannnnnnnnnnnnn/p/14444048.html)
6. (暫未發表敬請期待...)[JWT](https://www.cnblogs.com/Iannnnnnnnnnnnn/p/14444048.html)
Vue3 前端專案
暫未發表敬請期待...
# 本文簡介
本文為《Asp.Net Core3 + Vue3入坑教程》系列教程的後端開篇,主要介紹 Asp.Net Core Web後端專案的搭建流程與Swagger配置。
# Simple專案搭建流程與Swagger配置步驟
#### 新建專案
![](https://img2020.cnblogs.com/blog/870711/202102/870711-20210223134838323-1572585574.png)
![](https://img2020.cnblogs.com/blog/870711/202102/870711-20210223134933077-1461092230.png)
![](https://img2020.cnblogs.com/blog/870711/202102/870711-20210223134959773-847099790.png)
#### 引入Swagger Nuget包
![](https://img2020.cnblogs.com/blog/870711/202102/870711-20210223135730291-1179301585.png)
![](https://img2020.cnblogs.com/blog/870711/202102/870711-20210223140010388-566711377.png)
#### 配置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");
> ```
![](https://img2020.cnblogs.com/blog/870711/202102/870711-20210223140228232-1705073756.png)
![](https://img2020.cnblogs.com/blog/870711/202102/870711-20210223140212879-1201186870.png)
#### 新建資料夾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