ASP.NET Boilerplate is integrated to ASP.NET Web API Controllers via Abp.Web.Api nuget package. You can create regular ASP.NET Web API Controllers as you always do. Dependency Injection properly works for regular ApiControllers.
But you should derive your controllers from AbpApiController, which provides several benefits and better integrates to ASP.NET Boilerplate.
This is a simple api controller derived from AbpApiController:
public class UsersController : AbpApiController
{
}
AbpApiController defines L method to make localization easier. Example:
public class UsersController : AbpApiController
{
public UsersController()
{
LocalizationSourceName = "MySourceName";
}
public UserDto Get(long id)
{
var helloWorldText = L("HelloWorld");
//...
}
}
You should set LocalizationSourceName to make L method working. You can set it in your own base api controller class, to not repeat for each api controller.
Audit logs are not automatically written for api controllers. You should declare Audited attribute for api controller or action to enable it. See audit logs documentation for more.
You can use AbpApiAuthorize attribute for your api controllers or actions to prevent unauthorized users to use your controllers and actions. Example:
public class UsersController : AbpApiController
{
[AbpApiAuthorize("MyPermissionName")]
public UserDto Get(long id)
{
//...
}
}
AbpApiController also defines IsGranted method as a shortcut to check permissions.
See authorization documentation for more.
Web API actions are not unit of work by default. For example, if you need database connection to be open in your action, you need to explicitly declare UnitOfWork attribute as shown below:
public class UsersController : AbpApiController
{
private readonly IRepository<User, long> _userRepository;
public UsersController(IRepository<User, long> userRepository)
{
_userRepository = userRepository;
}
[UnitOfWork]
public virtual List<UserDto> Users(string filter)
{
var users = _userRepository
.GetAll()
.Where(u => u.UserName.StartsWith(filter))
.ToList();
//...
}
}
We declared UnitOfWork attribute. This is needed since repository's GetAll() method returns IQueryable, which requires an open database connection while using ToList() method (because of deferred execution of IQueryable). Notice that it should also be virtual (because interception can not work otherwise).
See unit of work documentation for more.
You can also use pre-injected AbpSession, EventBus, PermissionManager, PermissionChecker, SettingManager, FeatureManager, FeatureChecker, LocalizationManager, Logger, CurrentUnitOfWork base properties and more.