- 创建.Net 6 API 项目
- NuGet添加引用 这两个引用
Volo.Abp.AspNetCore
Volo.Abp.Swashbuckle 添加Module类
里面的代码/// <summary>
/// API模块
/// </summary>
[DependsOn(typeof(AbpAspNetCoreModule), typeof(AbpSwashbuckleModule))]
public class ABPStudyApiModule : AbpModule
{
public override async Task ConfigureServicesAsync(ServiceConfigurationContext context)
{
//注册相应的服务
var services = context.Services;
services.AddControllers();
services.AddEndpointsApiExplorer(); //MinimalAPI给swagger提供数据的服务(不适用MinimalAPI可以不用)
services.AddAbpSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "ABPStudyApiModule API", Version = "v1", Description = "Module 模块化 ABPStudyApiModule 自定义类"
});
options.DocInclusionPredicate((docName, description) => true);
options.CustomSchemaIds(type => type.FullName);
options.HideAbpEndpoints(); //隐藏ABP的默认端点
});
await Task.CompletedTask;
}
public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
{
var app = context.GetApplicationBuilder();
var env = context.GetEnvironment();
if (env.IsDevelopment()) //开发环境
{
app.UseSwagger();
app.UseSwaggerUI(options =>
options.SwaggerEndpoint("/swagger/v1/swagger.json", "ABPStudyApiModule API"));
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseRouting();
app.UseAuthorization();
app.UseConfiguredEndpoints(); //代替原来的 app.MapControllers();
await Task.CompletedTask;
}
}修改Program.cs
using ABP.Study.API;
var builder = WebApplication.CreateBuilder(args);
// 重新注册 configuration
await builder.Services.AddApplicationAsync<ABPStudyApiModule>(options =>
options.Services.ReplaceConfiguration(builder.Configuration));
var app = builder.Build();
await app.InitializeApplicationAsync(); //初始化自定义模块 http请求管道
await app.RunAsync();搞定运行项目,这个时候已经给API层整合了ABP
ABP学习一(.Net6 API 整合 ABP)
最新推荐文章于 2024-07-09 16:05:22 发布