Getting Started Series - the use of ABP in AspNet Core MVC Web Application in

This tutorial shows you how to get started with minimal dependencies start using ABP development.

Often you need to download a  startup template

Create a new project

  1. Using Visual Studio to create an empty AspNet Core Web Application:
  2. Select a blank template

You can choose another template, but I want to demonstrate it from a simple project.

Volo.Abp.AspNetCore.Mvc installation package

Volo.Abp.AspNetCore.Mvc ABP is integrated AspNet Core MVC package, install it into your project:

Install-Package Volo.Abp.AspNetCore.Mvc

Creating ABP module

ABP is a modular framework, it needs a start (root) module inherited fromAbpModule:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.AspNetCore.Modularity;
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();
            }

            app.UseMvcWithDefaultRoute();
        }
    }
}

AppModule The application started good name of the module (module also suggest that you start using this name).

Of ABP module package defines the classes, the modules may depend on other modules. In the above code  AppModule relies on  AbpAspNetCoreMvcModule after adding (Volo.Abp.AspNetCore.Mvc module present in the package). ABP install new packet DependsOnis common practice .

We configure ASP.NET Core pipeline in this class module instead Startup class.

Startup class

Next, modified ABP integrated modular system startup class:

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

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

            return services.BuildServiceProviderFromFactory();
        }

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

Modification  ConfigureServices method return value  IServiceProvider (default void). This allows us to modify the alternative AspNet Core dependency injection framework (see below Autofac integral part).  services.AddApplication<AppModule>()Add all services defined in all modules.

app.InitializeApplication() Call  Configure method to initialize and start the application

Hello World!

The above application is no feature that allows us to create a MVC controller to achieve some of the features:

using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;

namespace BasicAspNetCoreApplication.Controllers
{
    public class HomeController : AbpController
    {
        public IActionResult Index()
        {
            return Content("Hello World!");
        }
    }
}

If you run the application you will see "Hello World!" In the page.

Derived HomeController from AbpController instead of standard Controller class. This is not required, but AbpController class has useful base properties and methods to make your development easier.

From the AbpControllerderived HomeController rather than inherited from Controllerclass. Although it is not mandatory, but the AbpControllerclass has many useful properties and there are ways to make your development easier.

Use dependency injection framework Autofac

Although AspNet Core dependency injection (DI) for a basic system requirements, but Autofac provides properties and methods of implantation interception advanced functions, which are executed advanced application framework ABP functions necessary.

AspNet Core substituted with Autofac DI system and integrated into the ABP is very simple.

  1. Volo.Abp.Autofac installation package
    Install-Package Volo.Abp.Autofac
  2. Add  AbpAutofacModule dependent
    [DependsOn(typeof(AbpAspNetCoreMvcModule))]
    [DependsOn(typeof(AbpAutofacModule))] // 在模块上添加依赖AbpAutofacModule
    public class AppModule : AbpModule
    {
        ...
    }
    

     

  3. Modified Startupunder the category services.AddApplication<AppModule>();as follows:
    services.AddApplication<AppModule>(options =>
    {
        options.UseAutofac(); // 集成 Autofac
    });
    

     

  4. Updating  Program.csthe code is no longer used WebHost.CreateDefaultBuilder()method (because it uses the default DI container):
    public class Program
    {
       public static void Main(string[] args)
       {
           /*
               https://github.com/aspnet/AspNetCore/issues/4206#issuecomment-445612167
               CurrentDirectoryHelpers 文件位于: \framework\src\Volo.Abp.AspNetCore.Mvc\Microsoft\AspNetCore\InProcess\CurrentDirectoryHelpers.cs
               当升级到ASP.NET Core 3.0的时候将会删除这个类.
           */
           CurrentDirectoryHelpers.SetCurrentDirectory();
    
           BuildWebHostInternal(args).Run();
       }
        public static IWebHost BuildWebHostInternal(string[] args) =>
           new WebHostBuilder()
               .UseKestrel()
               .UseContentRoot(Directory.GetCurrentDirectory())
               .UseIIS()
               .UseIISIntegration()
               .UseStartup<Startup>()
               .Build();
    }
    
     

     

Published 87 original articles · won praise 69 · Views 600,000 +

Guess you like

Origin blog.csdn.net/S2T11Enterprise/article/details/103983879