- Overview
- Installation
- Usage
- Customization
- Navigation Menu
- Changing Layout
- Authentication
- Handling Files
- Overriding Views
- Customizing AutoCrudAdminController
- Troubleshooting
AutoCrudAdmin is a library that provides automatic CRUD (Create, Read, Update, Delete) screens and API endpoints for entity classes in an ASP.NET Core application.
Install the AutoCrudAdmin NuGet package in your ASP.NET Core project.
dotnet add package AutoCrudAdmin
-
Call
services.AddAutoCrudAdmin()
inConfigureServices()
inStartup.cs
. -
Call
app.UseAutoCrudAdmin()
inConfigure()
inStartup.cs
. -
You have CRUD views and operations for each entity in your DbContexts.
-
You can customize the default behavior by creating a controller that inherits from
AutoCrudAdminController<TEntity>
whereTEntity
is the entity class.
That's it! AutoCrudAdmin will automatically generate CRUD screens and API endpoints for the entities.
The CRUD screens will be available under /ControllerName
like /Products
, /Customers
etc. The API endpoints will be at /ControllerName/Create
, /ControllerName/Edit
etc.
AutoCrudAdmin provides options to customize the generated CRUD screens and API behavior.
The grid view that lists entities on the Index screen can be customized by overriding methods in the controller.
For example, to explicitly show certain columns:
public class ProductsController : AutoCrudAdminController<Product>
{
protected override IEnumerable<string> ShownColumnNames =>
new[] { "Name", "Price" };
}
To add a custom column:
protected override IEnumerable<CustomGridColumn<Product>> CustomColumns =>
new[]
{
new CustomGridColumn<Product>
{
Name = "Discount",
ValueFunc = p => p.CalculateDiscount().ToString("0.##")
}
};
See Customizing Grid for more details and options.
Similarly, the forms for Create and Edit actions can be customized via methods like:
ShownFormControlNames
HiddenFormControlNames
ShownFormControlNamesOnCreate
For example:
protected override IEnumerable<string> HiddenFormControlNames {
new[] { "Description" }
};
Hides the Description field.
See Customizing Forms for more details and options.
The GenerateFormControls
methods are the main way to customize the form fields on Create and Edit pages.
See the GenerateFormControls Methods section for details on customizing these methods.
Additional controller actions can be added and will show alongside the standard CRUD actions.
To perform validation, override the EntityValidators
method:
protected override IEnumerable<Func<Product, Product, ValidatorResult>> EntityValidators {
(existing, updated) => {
if (updated.Quantity < 0) {
return ValidatorResult.Error("Invalid quantity");
}
return ValidatorResult.Success();
}
};
See Validating Entities for more details.
Generate a menu with links to CRUD screens using the NavHelper
:
@foreach (var item in NavHelper.GetNavItems()) {
<li>
<a asp-controller="@item">@item</a>
</li>
}
The default layout is _AutoCrudAdmin_Layout
. To use a custom layout:
services.AddAutoCrudAdmin(options => {
options.LayoutName = "_CustomLayout";
});
Add authentication by providing IAutoCrudAuthFilter
implementations:
options.Authorization.Add(new MyAuthFilter());
See Authentication for examples.
The FormFilesContainer
class passes files from forms to API endpoints.
Files are available in AdminActionContext.Files
.
See Handling Files for more details.
Override default AutoCrudAdmin views by placing view files in:
/Views/AutoCrudAdmin/
The AutoCrudAdminController
base class has many virtual members that can customize CRUD behavior:
Customize grid columns:
protected override IEnumerable<CustomGridColumn<TEntity>> CustomColumns {..}
Add row actions:
protected override IEnumerable<GridAction> CustomActions {..}
Show/hide form fields:
protected override IEnumerable<string> ShownFormControlNames {..}
protected override IEnumerable<string> HiddenFormControlNames {..}
See API Reference for full customization options.
Some common issues:
- Ensure DbContext registered properly in
ConfigureServices()
- For navigation menu, controller must inherit from
AutoCrudAdminController
- Check for conflicts with other packages