-
Couldn't load subscription status.
- Fork 8
REST API with one line of code
Ushakov Michael edited this page Sep 22, 2025
·
8 revisions
This page describes how to use and what limitations Wissance.WebApiToolkit has when we are speaking about the one-line controller feature . Adding a controller in one line occurs without declaring a controller class, thereby we are free from declaring new types in the application and save our time on writing code.
This feature imply following limitations:
- At the current version, the 1-line controller feature is working only with the
EntityFarameworkrelated manager class. - For this feature, we are using SimplifiedEfModelManager therefore
DTOandEntityare the same class. This simplification reduces factory parameters. However, we will add one more option to pass the factory in a separate method too see - Temporarily we are not having authorization for one line controller until this issue is resolved
- Prepare
Entityclass implementedIModelIdentifiable<T> - Get a Controller
Assemblyvia theAddSimplifiedAutoControllerand pass:- Class that derives
DbContext(EntityFramework) - Name of Resource = Name of a Controller without the "Controller" suffix that is using in route ->
api/[Controller] - enum that specifies controller type:
ReadOnly|FullCrud|Bulk
- Class that derives
Example of an instantiation of a controller in one line, split to 2 for the better visual perception:
private void ConfigureWebApi(IServiceCollection services)
{
ServiceProvider provider = services.BuildServiceProvider();
// in one line
Assembly stationControllerAssembly = services.AddSimplifiedAutoController<StationEntity, Guid, EmptyAdditionalFilters>(provider.GetRequiredService<ModelContext>(), "Station", ControllerType.FullCrud, null, provider.GetRequiredService<ILoggerFactory>());
services.AddControllers().AddApplicationPart(sensorControllerAssembly).AddControllersAsServices();
}The full example is here