Module 3: ASP.NET Core Web App

Building web applications with ASP.NET Core.

Duration: 90 minutes

Learning Objectives

  • • Understand the MVC pattern in ASP.NET Core
  • • Create controllers and handle HTTP requests
  • • Build views with Razor syntax
  • • Implement model binding and validation
  • • Configure routing and middleware

Key Topics

MVC Pattern Implementation

Understanding Model-View-Controller architecture in web applications.

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
    
    [HttpPost]
    public IActionResult Submit(StudentModel model)
    {
        if (ModelState.IsValid)
        {
            // Process the form
            return RedirectToAction("Success");
        }
        return View(model);
    }
}

Controllers and Views

Handling HTTP requests and rendering responses.

@model StudentModel
@{
    ViewData["Title"] = "Student Registration";
}

<h2>Student Registration</h2>
<form asp-action="Submit" method="post">
    <div class="form-group">
        <label asp-for="Name"></label>
        <input asp-for="Name" class="form-control" />
        <span asp-validation-for="Name"></span>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

Model Binding and Validation

Automatically mapping form data to model properties and validating input.

public class StudentModel
{
    [Required(ErrorMessage = "Name is required")]
    [StringLength(50, ErrorMessage = "Name cannot exceed 50 characters")]
    public string Name { get; set; }
    
    [Required]
    [EmailAddress]
    public string Email { get; set; }
    
    [Range(18, 100, ErrorMessage = "Age must be between 18 and 100")]
    public int Age { get; set; }
}

Routing and Middleware

Configuring URL patterns and request processing pipeline.

// Startup.cs or Program.cs
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

Hands-on Exercises

Exercise 1: Student Management System

Create a web application to manage student records with CRUD operations.

Exercise 2: Contact Form

Build a contact form with validation and email sending functionality.

Exercise 3: Product Catalog

Develop a product listing page with search and filtering capabilities.