See our new Wiki!
This wiki is in legacy mode. Check our new wiki here!
Programming ASP Core
Official guides
- Build web APIs with ASP Core 3.0
- Tutorial VS Code - ASP Core 3.0
- Tutorial VS - ASP Core 3.0
- Controller action return types ASP Core 3.0
Sample Project
AspCore.SampleAPI (Clean Architecture / Domain / Behaviors / Queries / AutoMapper / Swagger / Functional testing / CI)
Vortex
Write elegant and testeable solutions on C# using a Monadic Framework. ASP Core joins the functional side!
Performance
- A kind of - Practical intro to Async APIs
- Async discussions:
- Use DbContextPooling to improve the performance: .Net Core 2.1
Other guides
- Global error handling in ASP Core
EF Core Hotspots
-
Startup.cs -> Configure Services
services.AddDbContext<MYContext>( options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), serverOptions => serverOptions.MigrationsAssembly("ASSEMBLYNAME")));
-
appsettings.json
{ "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=DBNAME;Trusted_Connection=True;MultipleActiveResultSets=true" }, ... }
-
DbContext:
public class MYContext : DbContext { public MYContext() { } public MYContext(DbContextOptions<MYContext> options) : base(options) { } public DbSet<...> .... }
-
Testing InMemoryDatabase provider:
new DbContextOptionsBuilder<MYContext>() .UseInMemoryDatabase(databaseName: NAME) .Options
AutoMapper DI
AutoMapper Extensions for Microsoft DI.
// Add this line to your startup.cs
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
ASP Core Web API Example
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
protected readonly IUsersRepository _repository;
public UsersController(IUsersRepository repository)
{
_repository = repository;
}
[HttpGet]
[ProducesResponseType(200)]
public async Task<ActionResult<IEnumerable<User>>> GetAllAsync()
{
return await _repository.GetAll();
}
[HttpGet("{username}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<ActionResult<User>> GetByIdAsync(string username)
{
var existingUser = await _repository.GetByUsername(username);
if (existingUser == null)
{
return NotFound();
}
return existingUser;
}
[HttpPost]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
public async Task<ActionResult<User>> CreateUserAsync(User user)
{
await _repository.AddUser(user);
return CreatedAtAction(nameof(GetById), new { username = user.Username }, user);
}
[HttpPut("{username}")]
[ProducesResponseType(204)]
[ProducesResponseType(404)]
public async Task<IActionResult> UpdateUserAsync(string username, User user)
{
var existingUser = await _repository.GetByUsername(username);
if (existingUser == null)
{
return NotFound();
}
existingUser.Nickname = user.Nickname;
existingUser.Email = user.Email;
await _repository.Update(existingUser);
return NoContent();
}
[HttpDelete("{username}")]
[ProducesResponseType(204)]
[ProducesResponseType(404)]
public async Task<IActionResult> DeleteUserAsync(string username)
{
var existingUser = await _repository.GetByUsername(username);
if (existingUser == null)
{
return NotFound();
}
await _repository.Delete(existingUser);
return NoContent();
}
}
Equilaterus (CC-BY) 2018 - 2022.