Learn to build modern web applications with ASP.NET Core framework.
Learn to build modern web applications with ASP.NET Core framework.
Understand the architecture and components of ASP.NET Core applications
Content by: Akash Vadher
.Net Developer
// Program.cs (Minimal Hosting Model)
var builder = WebApplication.CreateBuilder(args);
// Add services to the container
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
The ASP.NET Core request pipeline is a series of middleware components that process HTTP requests and responses. Each middleware can perform operations before and after the next middleware in the pipeline.
Test your understanding of this topic:
Learn both traditional controllers and modern minimal APIs for building web endpoints
Content by: Vaibhav Nakrani
.Net Developer
// Simple GET endpoint
app.MapGet("/hello", () => "Hello World!");
// GET with parameters
app.MapGet("/hello/{name}", (string name) => $"Hello {name}!");
// POST with JSON body
app.MapPost("/users", (User user) => Results.Created($"/users/{user.Id}", user));
// PUT with validation
app.MapPut("/users/{id}", (int id, User user) =>
{
// Update user logic
return Results.Ok(user);
});
// DELETE endpoint
app.MapDelete("/users/{id}", (int id) =>
{
// Delete user logic
return Results.NoContent();
});
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
private readonly IUserService _userService;
public UsersController(IUserService userService)
{
_userService = userService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<User>>> GetUsers()
{
var users = await _userService.GetAllUsersAsync();
return Ok(users);
}
[HttpGet("{id}")]
public async Task<ActionResult<User>> GetUser(int id)
{
var user = await _userService.GetUserByIdAsync(id);
if (user == null)
return NotFound();
return Ok(user);
}
[HttpPost]
public async Task<ActionResult<User>> CreateUser(CreateUserRequest request)
{
var user = await _userService.CreateUserAsync(request);
return CreatedAtAction(nameof(GetUser), new { id = user.Id }, user);
}
}
Test your understanding of this topic:
Master model binding, validation, and data annotation in ASP.NET Core
Content by: Dipen Dalvadi
.Net Developer
// From query string
public IActionResult GetUsers([FromQuery] int page, [FromQuery] int size)
{
// page and size are bound from query parameters
}
// From route
[HttpGet("{id}")]
public IActionResult GetUser([FromRoute] int id)
{
// id is bound from route parameter
}
// From body
[HttpPost]
public IActionResult CreateUser([FromBody] CreateUserRequest request)
{
// request is bound from JSON body
}
// From form
[HttpPost]
public IActionResult UploadFile([FromForm] IFormFile file)
{
// file is bound from form data
}
public class CreateUserRequest
{
[Required(ErrorMessage = "Name is required")]
[StringLength(100, MinimumLength = 2)]
public string Name { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Range(18, 120, ErrorMessage = "Age must be between 18 and 120")]
public int Age { get; set; }
[Phone]
public string? PhoneNumber { get; set; }
[RegularExpression(@"^[A-Z]{2}d{6}$", ErrorMessage = "Invalid ID format")]
public string? NationalId { get; set; }
}
// Custom validation attribute
public class ValidAgeAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
if (value is int age)
{
return age >= 18 && age <= 120;
}
return false;
}
}
Test your understanding of this topic:
Continue your learning journey and master the next set of concepts.
Continue to Module 4