ASP.NET Core Web API – Intermediate Concepts

16. What is Entity Framework Core (EF Core)?

EF Core is a lightweight ORM that allows database operations using C# objects and LINQ.


public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}
public class AppDbContext : DbContext
{
    public DbSet Products { get; set; }
}
    

17. What is DbContext?

Manages DB connection and entity tracking.


public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions options) : base(options) { }
    public DbSet Products { get; set; }
}
    

18. What is LINQ?

Query data using C# syntax.


var data = _context.Products
    .Where(p => p.Price > 1000)
    .ToList();
    

19. Async/Await

Non-blocking operations improve performance.


[HttpGet]
public async Task GetProducts()
{
    var products = await _context.Products.ToListAsync();
    return Ok(products);
}    

20. DTO

DTOs are used to transfer only necessary data between layers, improving performance and security.


public class ProductDto
{
    public string Name { get; set; }
    public decimal Price { get; set; }
}
    

21. AutoMapper

Maps objects automatically.


CreateMap();
var dto = _mapper.Map(product);
    

22. Logging

Tracks events and errors.


_logger.LogInformation("Fetched products successfully.");
_logger.LogError("Error fetching products.");
    

23. CORS

Allows cross-domain API calls.


app.UseCors("AllowAll");
    

24. Authentication

Verifies user identity.


[Authorize]
public IActionResult Secure() => Ok();
    

25. Authorization

Checks user roles/permissions.


[Authorize(Roles="Admin")]
    

26. JWT

Token-based authentication.


var claims = new[] { new Claim(ClaimTypes.Name, "user") };
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("secret_key"));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

var token = new JwtSecurityToken(
    claims: claims,
    expires: DateTime.Now.AddHours(1),
    signingCredentials: creds
);
    

27. API Versioning

Supports multiple API versions.


[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/products")]
public class ProductsController : ControllerBase { }    

28. Pagination

Fetch data in chunks.


.Skip(0).Take(10)
    

29. Filtering


.Where(p => p.Price > 500)
    

30. Sorting


.OrderBy(p => p.Name)
    

31. Repository Pattern

Separates data access logic.

32. Unit Testing

Unit testing validates individual methods or classes to ensure correctness.

33. Caching

Caching stores frequently used data in memory to improve performance and reduce database load.


var products = _cache.GetOrCreate("products", entry =>
{
    entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5);
    return _context.Products.ToList();
});
    

34. PUT vs PATCH

PUT = full update, PATCH = partial update.

35. HttpClient

Used to call external APIs.


var client = new HttpClient();
var response = await client.GetStringAsync("https://api.example.com/products");    

Intermediate Quiz

Basic (1–15) Intermediate (16–35) Advance (36-50) Most Asking(51-100)