开发环境:VS2022、.NET6
1、创建项目:BasicAspNetCoreApplication
2、NuGet添加:Volo.Abp.AspNetCore.Mvc和Volo.Abp.Autofac,如下图所示:
3、开始写代码,目录如下图所示:
3.1、添加启动模块AppModule.cs
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;
namespace BasicAspNetCoreApplication
{
[DependsOn(typeof(AbpAspNetCoreMvcModule))]
[DependsOn(typeof(AbpAutofacModule))] //在模块上添加AbpAutofacModule
public class AppModule:AbpModule
{
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var app = context.GetApplicationBuilder();
var env=context.GetEnvironment();
if (env.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseConfiguredEndpoints();
base.OnApplicationInitialization(context);
}
}
}
3.2、修改Program.cs
using BasicAspNetCoreApplication;
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseAutofac(); //添加AbpAutofacModule
builder.Services.ReplaceConfiguration(builder.Configuration);
builder.Services.AddApplication<AppModule>();
var app=builder.Build();
app.InitializeApplication();
app.Run();