-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
121 lines (99 loc) · 4.47 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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;
using Swagger2Docx.Helper;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Swagger2Docx
{
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)
{
services.AddScoped<SwaggerGenerator>(); //注入SwaggerGenerator,後面可以直接使用這個方法
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1", //版本
Title = "Swagger2Docx", //標題
Description = $"項目名稱 Http API v1", //描述
});
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//獲取應用程式所在目錄(絕對,不受工作目錄影響,建議採用此方法獲取路徑)
//var basePath = AppContext.BaseDirectory;
var xmlPath = Path.Combine(basePath, "docTemp.xml"); //這個就是剛剛配置的xml檔案名
// c.IncludeXmlComments(xmlPath);//默認的第二個參數是false,對方法的注釋
c.IncludeXmlComments(xmlPath, true); // 這個是controller的注釋
});
services.AddScoped<SpireDocHelper>();
services.AddControllers();
//services.AddControllers();
//services.AddSwaggerGen(c =>
//{
// c.SwaggerDoc("v1", new OpenApiInfo { Title = "Swagger2Docx", Version = "v1" });
//});
}
// 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();
#region Swagger 只在開發環節中使用
app.UseSwagger();
app.UseSwaggerUI(c => {
//c.SwaggerEndpoint($"/swagger/v1/swagger.json", $"Swagger2Docx v1");
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Swagger2Docx v1");
//c.RoutePrefix = string.Empty; //如果是為空 訪問路徑就為 根功能變數名稱/index.html,注意localhost:8001/swagger是訪問不到的
//路徑配置,設置為空,表示直接在根功能變數名稱(localhost:8001)訪問該檔
// c.RoutePrefix = "swagger"; // 如果你想換一個路徑,直接寫名字即可,比如直接寫c.RoutePrefix = "swagger"; 則訪問路徑為 根功能變數名稱/swagger/index.html
c.DocumentTitle = "項目名稱 線上文檔調試";
#region 自訂樣式
//css 注入
c.InjectStylesheet("/css/swaggerdoc.css");
c.InjectStylesheet("/css/app.min.css");
//js 注入
c.InjectJavascript("/js/jquery.js");
c.InjectJavascript("/js/swaggerdoc.js");
c.InjectJavascript("/js/app.min.js");
#endregion
});
#endregion
}
app.UseRouting();
//app.UseAuthorization();
app.UseStaticFiles();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
//if (env.IsDevelopment())
//{
// app.UseDeveloperExceptionPage();
// app.UseSwagger();
// app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Swagger2Docx v1"));
//}
//app.UseRouting();
////app.UseAuthorization();
//app.UseEndpoints(endpoints =>
//{
// endpoints.MapControllers();
//});
}
}
}