ASP.NET Boilerplate is integrated to ASP.NET MVC Controllers via Abp.Web.Mvc nuget package. You can create regular MVC Controllers as you always do. Dependency Injection properly works for regular MVC Controllers.
But you should derive your controllers from AbpController, which provides several benefits and better integrates to ASP.NET Boilerplate.
This is a simple controller derived from AbpController:
public class HomeController : AbpController
{
public ActionResult Index()
{
return View();
}
}
AbpController defines L method to make localization easier. Example:
public class HomeController : AbpController
{
public HomeController()
{
LocalizationSourceName = "MySourceName";
}
public ActionResult Index()
{
var helloWorldText = L("HelloWorld");
return View();
}
}
You should set LocalizationSourceName to make L method working. You can set it in your own base controller class, to not repeat for each controller.
Exceptions are automatically handled, logged and a proper response is returned to the client. See exception handling documentation for more.
ASP.NET Boilerplate wraps action results by default if return type is JsonResult (or Task<JsonResult> for async actions). You can change this by using WrapResult and DontWrapResult attributes for controllers or actions. See ajax documentation for more.
Audit logs are automatically written if you derive from AbpController. See audit logs documentation for more.
You can use AbpMvcAuthorize attribute for your controllers or actions to prevent unauthorized users to use your controllers and actions. Example:
public class HomeController : AbpController
{
[AbpMvcAuthorize("MyPermissionName")]
public ActionResult Index()
{
return View();
}
}
AbpController also defines IsGranted method as a shortcut to check permissions.
See authorization documentation for more.
MVC 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 HomeController : AbpController
{
private readonly IRepository<User, long> _userRepository;
public HomeController(IRepository<User, long> userRepository)
{
_userRepository = userRepository;
}
[UnitOfWork]
public virtual ActionResult Users(string filter)
{
var users = _userRepository
.GetAll()
.Where(u => u.UserName.StartsWith(filter))
.ToList();
return View(users);
}
}
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.