This is a step-by-step example of building a ASP.NET Core Web application using NebolController 1.0.0.30
Create a new project using the “ASP.NET Core Web Application” template. Select the “API” version to get some basic scaffolding. I am using .NET Core version is 3. I unchecked the “Configure for HTTPS” option.
You will probably get a sample controller named WeatherForecastController.cs, feel free to delete that one, and in Project Properties –> Debug dialog replace the “weatherforecast” string in the “Launch Browser” editbox with “api/book”. That will work later.
Add nuget packages
If you haven't done so already, add the nuget.nebol.se repository to Visual Studio by running this in Package Manager:
nuget sources add -name nuget.nebol.se -source https://nuget.nebol.se/api/odata
Then add the packages:
Install-Package NebolBooks Install-Package NebolController Install-Package SharpRepository.Ioc.Microsoft.DependencyInjection Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection -version 6.1.1 Install-Package SharpRepository.InMemoryRepository
The reason for using version 6.1.1 of AutoMapper.Extensions.Microsoft.DependencyInjection is that the next version (7.0.0) uses AutoMapper 9, and NebolController is not configured to work with 9, only with 8.
Install-Package Autofac.Extensions.DependencyInjection -version 5.0.1 Install-Package SharpRepository.Ioc.Autofac
The reason for using version 5.0.1 of Autofac.Extensions.DependencyInjection is that the next version 6.0.0 uses AutoFac >= 5.0.0 and SharpRepository.Ioc.Autofac 2.1.0-prerelease requires AutoFac < 5.0.0
We'll add some view models
add code
Add a BookRepositoryHelper that gets the books from NebolBooks and loads them into the in-memory repository.
add code
Add specifications
add code
Add profile
add code
Add controller
Add SharpRepository setup in app.settings
Changes to Startup.ConfigureServices:
change return type from void to IServiceProvider services.AddAutoMapper… return services.UseSharpRepository(Configuration.GetSection(“sharpRepository”));
additions to Configure()…
As this is a .NET Core 3.1 application we need to add a supported DI package and do the steps described here: https://github.com/SharpRepository/SharpRepository/wiki/Getting-started in the section titled “ASP.NET Core 3.0 Applications”
You know what? Let's add some finishing touches.
In Startup.ConfigureServices() add
services.AddResponseCompression();
In Startup.Configure() add
app.UseResponseCompression();
Taken from here: Get started with Swashbuckle and ASP.NET Core
Install-Package Swashbuckle.AspNetCore
In Startup.ConfigureServices() add
services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" }); });
In Startup.Configure(), perhaps inside "if (env.IsDevelopment())" add
app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1"); });
In Startup.ConfigureServices() add
For .NET Core 3.0+:
services.AddMvc() .AddJsonOptions(options => { options.JsonSerializerOptions.WriteIndented = true; //options.SerializerSettings.Formatting = Formatting.Indented; // NewtonsoftJson });
Now try to run it, and visit /swagger to see the automatically generated API documentation!