首先nuget引入包
Swashbuckle.AspNetCore.Swagger
Swashbuckle.AspNetCore.SwaggerGen
Swashbuckle.AspNetCore.SwaggerUI
然后進行服務定義及管道注入
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)
{
//Swagger引入Swashbuckle.AspNetCore
services.AddSwaggerGen(c =>
{
//new Swashbuckle.AspNetCore.Swagger.Contact
//new Swashbuckle.AspNetCore.Swagger.Info
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1.1.0",
Title = "apicore",
Description = "信息信息",
//TermsOfService = "None",
Contact = new OpenApiContact() { Name = "rrrrr", Email = "cccccc@163.com", Url =new Uri("http://www.xxxxx.com") }
});
// 為 Swagger JSON and UI設置xml文檔注釋路徑
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//獲取應用程序所在目錄(絕對,不受工作目錄影響,建議采用此方法獲取路徑)
//var basePath = Path.GetDirectoryName(AppContext.BaseDirectory);//3.0
//定義實體類XML的路徑
//var entityXmlPath = Path.Combine(basePath, "CommonService.xml");
//如果需要顯示控制器注釋只需將第二個參數設置為true
c.IncludeXmlComments(xmlPath, true);
//添加實體的注釋
//c.IncludeXmlComments(entityXmlPath);
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHost host, Microsoft.Extensions.Hosting.IHostApplicationLifetime appLitetime)
{
//using (var container = host.Services.CreateScope())
//{
// IPhone phone = container.ServiceProvider.GetService<IPhone>();
//}
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();//????需要添加基礎標識數據庫架構之后才用與數據遷移
}
else
{
//app.UseExceptionHandler("/Home/Error");
//the default hsts value is 30 days,you may want to change this for production scenarios,see https://aka.ms/aspnetcore-hsts.
//app.UseHsts();
}
//app.UseHttpsRedirection();
//app.UseSession();//app.UseSession();必須在app.UseHttpsRedirection();之后
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseRouting();
app.UseCors("default");//跨域管道
//app.UseAuthentication();//認證管道
app.UseAuthorization();//
//Swagger
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "WyApiCoreHelp V1");
c.RoutePrefix = string.Empty;//如果設置更目錄為swagger,將此值置空,可在 http://localhost:<port>/ 找到 Swagger UI
//c.RoutePrefix = “swagger”;決定訪問Swagger UI的路徑,可在 http://localhost:<port>/swagger 找到 Swagger UI
});
//啟動應用,並導航到 http://localhost:<port>/swagger/v1/swagger.json。 生成的描述終結點的文檔顯示如下json格式。
//可在 http://localhost:<port>/swagger 找到 Swagger UI。 http://localhost:<port>/index.html通過 Swagger UI 瀏覽 API文檔
//app.UseMvc();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
////區域路由
//endpoints.MapAreaControllerRoute(
// name: "areas",
// areaName:"areas",
// pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
//endpoints.MapRazorPages();
});
}
//
private X509Certificate2 GetCertificate()
{
var assembly = typeof(Startup).GetTypeInfo().Assembly;
using (var stream = assembly.GetManifestResourceStream(
assembly.GetManifestResourceNames().First(r => r.EndsWith("wyrjgs.pfx"))))
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
return new X509Certificate2(bytes);
}
}
}
