This document describes ASP.NET Core integration for ASP.NET Boilerplate framework. ASP.NET Core integration is implemented in Abp.AspNetCore nuget package
Still working to create a simple startup template. You can take it as a starting point for your projects or you can follow this document to integrate ABP into your ASP.NET Core MVC projects.
To integrate ABP to ASP.NET Core, we should make some changes in the Startup class as shown below:
public class Startup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//...
services.AddMvc(mvcOptions =>
{
//Add ABP infrastructure to MVC
mvcOptions.AddAbp(services);
//...
});
//Configure Abp and Dependency Injection. Should be called last.
return services.AddAbp<MyProjectWebModule>(options =>
{
//Configure Log4Net logging (optional)
options.IocManager.IocContainer.AddFacility<LoggingFacility>(
f => f.UseLog4Net().WithConfig("log4net.config")
);
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//Initializes ABP framework and all modules. Should be called first.
app.UseAbp();
//...
}
}
You can use startup configuration to configure AspNet Core Module (using Configuration.Modules.AbpAspNetCore() in PreInitialize of your module).
Controllers can be any type of classes in ASP.NET Core. It's not restricted to classes derived from Controller class. By default, a class ends with Controller (like ProductController) is considered as MVC Controller. You can also add MVC's [Controller] attribute to any class to make it a controller. This is the way ASP.NET Core MVC is working. See ASP.NET Core documentation for more.
If you will use web layer classes (like HttpContext) or return a view, it's better to inherit from AbpController (which is derived from MVC's Controller) class. But if you are creating an API controller just works with objects, you can consider to create a POCO controller class or you can use your application services as controllers as described below.
ASP.NET Boilerplate provides infrastructure to create application services. If you want to expose your application services to remote clients as controllers (as previously done using dynamic web api), you can easily do it by a simple configuration in PreInitialize method of your module. Example:
Configuration.Modules.AbpAspNetCore().CreateControllersForAppServices(typeof(MyApplicationModule).Assembly, moduleName: 'app', useConventionalHttpVerbs: true);
CreateControllersForAppServices method gets an assembly and converts all application services to MVC controllers in that assembly. You can use RemoteService attribute to enable/disable it for method or class level.
When an application service is converted to MVC Controller, it's default route will be like /api/services/<module-name>/<service-name>/<method-name>. Example: If ProductAppService defines a Create method, it's URL will be /api/services/app/product/create (assumed that module name is 'app').
If useConventionalHttpVerbs set to true (which is the default value), then HTTP verbs for service methods are determined by naming conventions:
You can use any ASP.NET Core attributes to change HTTP methods or routes of the actions (but surely, this requires to add reference to related ASP.NET Core package).
Note: Previously, dynamic web api system was requiring to create service interfaces for application services. But this is not required for ASP.NET Core integration. Also, MVC attributes should be added to the service classes, even you have interfaces.
ABP defines some pre-built filters for AspNet Core. All of them are added to all actions of all controllers by default.
AbpAuthorizationFilter is used to integrate to authorization system and feature system.
AbpAuditActionFilter is used to integrate to audit logging system. It logs all requests to all actions by default (if auditing is not disabled). You can control audit logging using Audited and DisableAuditing attributes for actions and controllers.
AbpValidationActionFilter is used to integrate to validation system and automatically validate all inputs of all actions. In addition to ABP's built-in validation & normalization, it also checks MVC's Model.IsValid property and throws validation exception if action inputs have any invalid value.
You can control validation using EnableValidation and DisableValidation attributes for actions and controllers.
AbpUowActionFilter is used to integrate to Unit of Work system. It automatically begins a new unit of work before an action execution and completes unit of work after action exucition (if no exception is thrown).
You can use UnitOfWork attribute to control behaviour of UOW for an action. You can also use startup configuration to change default unit of work attribute for all actions.
AbpExceptionFilter is used to handle exceptions thrown from controller actions. It handles and logs exceptions and returns wrapped response to the client.
AbpResultFilter is used to wrap result action if action is successfully executed.
AbpDateTimeModelBinder is used to normalize DateTime (and Nullable<DateTime>) inputs using Clock.Normalize method.
MVC Views can be inherited from AbpRazorPage to automatically inject most used infrastructure (LocalizationManager, PermissionChecker, SettingManager... etc.). It also has shortcut methods (like L(...) for localize texts). Startup template inherits it by default.
ABP can automatically create javascript proxies for all MVC Controllers (not only application services). It's created for Application Services as Controllers (see the section above) by default. You can add [RemoteService] attribute to any MVC controller to create client proxy for it. Javascript proxies are dynamically generated on runtime. You need to add given script definition to your page:
<script src="~/AbpServiceProxies/GetAll?type=jquery" type="text/javascript"></script>
Currently, only JQuery proxies are generated. We can then call an MVC method with javascript as shown below:
abp.services.app.product.create({
name: 'My test product',
price: 99
}).done(function(result){
//...
});
Integration testing is fairly easy for ASP.NET Core and it's documented it's own web site in details. ABP follows this guide and provides AbpAspNetCoreIntegratedTestBase class in Abp.AspNetCore.TestBase package. It makes integration testing even easier.
It's better to start by investigating integration tests in startup template to see it in action.