Microsoft .NET/ASP.Net & ASP.Net Core
[ASP.Net Core / Blazor] Swagger 연동
전자기린
2020. 3. 16. 11:01
aspnetcore_swagger_sample.zip
0.14MB
ASP.Net Core에서 Swagger 사용하기 - nuget에서 Swashbuckle.AspNetCore 설치
더보기

프로젝트 속성 - 빌드 - XML 문서 파일 항목 체크(summary값 xml로 출력)

Startup.cs의 ConfigureServices 함수에 "#region Create Swagger Document" 붙여넣기
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
////////////////////////////////////////////////////////////////////////
#region Create Swagger Document
//스웨거 문서정보 생성.
services.AddSwaggerGen(o =>
{
o.SwaggerDoc("v1"
, new Microsoft.OpenApi.Models.OpenApiInfo()
{
Title = "aspnetcore_swagger_sample",
Description = "swagger 사용을 위한 예제",
Version="V1",
License = new Microsoft.OpenApi.Models.OpenApiLicense()
{
Name="virtualgiraffe.tistory.com",
Url= new Uri("http://virtualgiraffe.tistory.com")
}
});
//애플리케이션의 기본 경로
var xmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = System.IO.Path.Combine(AppContext.BaseDirectory, xmlFile);
//xml 경로
o.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);
});
#endregion
////////////////////////////////////////////////////////////////////////
}
Startup.cs의 Configure함수에 "#region Swagger UI" 붙여넣기
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
////////////////////////////////////////////////////////////////////////
#region Swagger UI
//스웨거 미들웨어 설정
app.UseSwagger();
//스웨거 UI 활성화
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
//접속 경로 / 미입력 시 swagger
//c.RoutePrefix = "swagger-sample";
});
#endregion
////////////////////////////////////////////////////////////////////////
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
host/[c.RoutePrefix 값] 으로 접근
Blazor에서 Swagger 사용하기 - nuget에서 Swashbuckle.AspNetCore 설치
더보기
Program.cs - line 9에 아래 내용 추가
// Swagger
builder.Services.AddSwaggerGen();
Program.cs - line 33에 아래 내용 추가
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Blazor API V1");
});
using Microsoft.AspNetCore.ResponseCompression;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
#region Swagger - 1
builder.Services.AddSwaggerGen();
#endregion
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/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.UseBlazorFrameworkFiles();
app.UseStaticFiles();
#region Swagger - 2
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Blazor API V1");
});
#endregion
app.UseRouting();
app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("index.html");
app.Run();