net core 3.0 与swagger
首先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);
}
}
}