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.8.0+):

  1. Configure your new project:

  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:

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:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Modularity;

namespace BasicAspNetCoreApplication
{
    [DependsOn(typeof(AbpAspNetCoreMvcModule))]
    public class AppModule : AbpModule
    {
        public override void OnApplicationInitialization(
            ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var env = context.GetEnvironment();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();
            app.UseRouting();
            app.UseConfiguredEndpoints();
        }
    }
}

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:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace BasicAspNetCoreApplication
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddApplication<AppModule>();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.InitializeApplication();
        }
    }
}

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
Install-Package Volo.Abp.Autofac
  1. Add AbpAutofacModule Dependency
[DependsOn(typeof(AbpAspNetCoreMvcModule))]
[DependsOn(typeof(AbpAutofacModule))] //Add dependency to ABP Autofac module
public class AppModule : AbpModule
{
    ...
}
  1. Update Program.cs to use Autofac:
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace BasicAspNetCoreApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })
                .UseAutofac(); //Add this line
    }
}

Source Code

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

Contributors


Last updated: January 01, 0001 Edit this page on GitHub

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

In this document
Community Talks

What’s New with .NET 9 & ABP 9?

21 Nov, 17:00
Online
Watch the Event
Mastering ABP Framework Book
Mastering ABP Framework

This book will help you gain a complete understanding of the framework and modern web application development techniques.

Learn More