Getting Started ABP With AspNet Core MVC Web Application

This tutorial explains how to start ABP from scratch with minimal dependencies. You generally want to start with the startup template.

Create A New Project

  1. Create a new AspNet Core Web Application from Visual Studio 2019 (16.4.0+):

Empty Web Project - 图1

  1. Configure your new project:

Empty Web Project - 图2

  1. Press to the create button:

create-aspnet-core-application

Install Volo.Abp.AspNetCore.Mvc Package

Volo.Abp.AspNetCore.Mvc is AspNet Core MVC integration package for ABP. So, install it to your project:

  1. Install-Package Volo.Abp.AspNetCore.Mvc

Create First ABP Module

ABP is a modular framework and it requires a startup (root) module class derived from AbpModule:

  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.Extensions.Hosting;
  3. using Volo.Abp;
  4. using Volo.Abp.AspNetCore.Mvc;
  5. using Volo.Abp.Modularity;
  6. namespace BasicAspNetCoreApplication
  7. {
  8. [DependsOn(typeof(AbpAspNetCoreMvcModule))]
  9. public class AppModule : AbpModule
  10. {
  11. public override void OnApplicationInitialization(
  12. ApplicationInitializationContext context)
  13. {
  14. var app = context.GetApplicationBuilder();
  15. var env = context.GetEnvironment();
  16. if (env.IsDevelopment())
  17. {
  18. app.UseDeveloperExceptionPage();
  19. }
  20. else
  21. {
  22. app.UseExceptionHandler("/Error");
  23. }
  24. app.UseStaticFiles();
  25. app.UseRouting();
  26. app.UseConfiguredEndpoints();
  27. }
  28. }
  29. }

AppModule is a good name for the startup module for an application.

ABP packages define module classes and a module can depend on another. In the code above, the AppModule depends on the AbpAspNetCoreMvcModule (defined by Volo.Abp.AspNetCore.Mvc package). It’s common to add a DependsOn attribute after installing a new ABP nuget package.

Instead of the Startup class, we are configuring ASP.NET Core pipeline in this module class.

The Startup Class

Next step is to modify Startup class to integrate to ABP module system:

  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.Extensions.DependencyInjection;
  3. namespace BasicAspNetCoreApplication
  4. {
  5. public class Startup
  6. {
  7. public void ConfigureServices(IServiceCollection services)
  8. {
  9. services.AddApplication<AppModule>();
  10. }
  11. public void Configure(IApplicationBuilder app)
  12. {
  13. app.InitializeApplication();
  14. }
  15. }
  16. }

services.AddApplication<AppModule>() adds all services defined in all modules starting from the AppModule.

app.InitializeApplication() in Configure method initializes and starts the application.

Run the Application!

That’s all! Run the application, it will just work as expected.

Using Autofac as the Dependency Injection Framework

While AspNet Core’s Dependency Injection (DI) system is fine for basic requirements, Autofac provides advanced features like Property Injection and Method Interception which are required by ABP to perform advanced application framework features.

Replacing AspNet Core’s DI system by Autofac and integrating to ABP is pretty easy.

  1. Install Volo.Abp.Autofac package
  1. Install-Package Volo.Abp.Autofac
  1. Add AbpAutofacModule Dependency
  1. [DependsOn(typeof(AbpAspNetCoreMvcModule))]
  2. [DependsOn(typeof(AbpAutofacModule))] //Add dependency to ABP Autofac module
  3. public class AppModule : AbpModule
  4. {
  5. ...
  6. }
  1. Update Program.cs to use Autofac:
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.Extensions.Hosting;
  3. namespace BasicAspNetCoreApplication
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. CreateHostBuilder(args).Build().Run();
  10. }
  11. public static IHostBuilder CreateHostBuilder(string[] args) =>
  12. Host.CreateDefaultBuilder(args)
  13. .ConfigureWebHostDefaults(webBuilder =>
  14. {
  15. webBuilder.UseStartup<Startup>();
  16. })
  17. .UseAutofac(); //Add this line
  18. }
  19. }

Source Code

Get source code of the sample project created in this tutorial from here.