Skip to content

Latest commit

 

History

History
163 lines (142 loc) · 12.6 KB

File metadata and controls

163 lines (142 loc) · 12.6 KB

Learn ASP.NET Core 8.0 - Full Course for Beginners [Tutorial]

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

Introduction to the Tutorial

  • Summary: The tutorial focuses on building a minimum viable project (MVP) for CRUD operations in ASP.NET Core 8.0 using a decoupled architecture with Blazor on the frontend and a RESTful API on the backend. It condenses typical long tutorials into about 3.5 hours, emphasizing efficiency for beginners while covering data persistence in a SQL Server database.
  • Key Takeaway/Example: Use Blazor for a responsive single-page app feel, similar to React or Angular, and separate concerns for backend (API) and frontend developers. Data validation occurs on both ends, with Bootstrap for UI styling.
  • Link for More Details: Ask AI: ASP.NET Core MVP Setup

Tools and Project Setup

  • Summary: Start with Visual Studio Community 2022, installing the ASP.NET and web development workload. Create an empty ASP.NET Core Web API project for the backend, placed in a folder like "FullStackMovies", and run it to verify "Hello World" output.
  • Key Takeaway/Example: No prior knowledge of HTML, CSS, JavaScript, or C# is required, but familiarity helps. The setup ensures a clean slate without excess template code.
  • Link for More Details: Ask AI: Visual Studio ASP.NET Setup

Creating Entities and Database with Entity Framework

  • Summary: Install Entity Framework Core Tools and SQL Server provider via NuGet. Define Movie and Genre classes as entities with properties like ID, Name, Price, ReleaseDate, and a one-to-one relationship via navigation and foreign key properties. Configure the connection string in appsettings.json for localDB, set up MovieContext as DbContext, and seed initial data.
  • Key Takeaway/Example: Use data annotations for validation (e.g., [Required], [MaxLength(50)], [Range(1, 100)]). Run migrations with "add-migration Initial" and "update-database" to create tables and insert seed data.
public class Movie
{
    public int Id { get; set; }
    [Required, MaxLength(50)]
    public string Name { get; set; }
    [Required, Range(1, 100)]
    public decimal Price { get; set; }
    public DateOnly ReleaseDate { get; set; }
    [ValidateNever]
    public Genre Genre { get; set; }
    public int GenreId { get; set; }
}

RESTful API Principles and Endpoints

  • Summary: Quick overview of REST principles: stateless HTTP requests for resources using GET, POST, PUT, DELETE (CRUD), with status codes and URI conventions. Create MoviesEndpoints class with extension methods for routes like /movies (GET all), /movies/{id} (GET by ID, PUT update, DELETE), and POST for adding. Use dependency injection for MovieContext.
  • Key Takeaway/Example: Handle not found with Results.NotFound() for GET by ID. Add validation with MinimalApis.Extensions. Test endpoints using VS HTTP files or Postman.
group.MapGet("/{id}", async (MovieContext movieContext, int id) =>
{
    var movie = await movieContext.Movies.Include(m => m.Genre).FirstOrDefaultAsync(m => m.Id == id);
    return movie is null ? Results.NotFound() : Results.Ok(movie);
});

Setting Up Blazor Frontend

  • Summary: Create a Blazor Web App project, configure services in Program.cs with AddHttpClient for MoviesClient and GenresClient to call API endpoints. Use dependency injection to pass clients to components.
  • Key Takeaway/Example: Set base address in Program.cs for API calls. Render mode set to InteractiveServer for dynamic interactions.
builder.Services.AddHttpClient<MoviesClient>(client => client.BaseAddress = new Uri("https://localhost:7027/"));

Implementing Read Operation in Blazor

  • Summary: In Home.razor, inject MoviesClient, load movies on initialization, and display in Bootstrap cards with grid system for responsiveness. Fetch data asynchronously from API.
  • Key Takeaway/Example: Use @foreach to loop movies into cards showing name, genre, price, and release date. Handle loading state with conditional rendering.
@if (movies == null)
{
    <p class="text-center">Loading...</p>
}
else
{
    <div class="row row-cols-1 row-cols-md-3 g-4">
        @foreach (var movie in movies)
        {
            <div class="card text-center border-primary">
                <!-- Card content -->
            </div>
        }
    </div>
}

Customizing Navbar with Bootstrap

  • Summary: Modify NavMenu.razor to use Bootstrap navbar across the top, remove sidebar, add CDN for Bootstrap JS and icons. Adjust CSS and disable launch browser in properties.
  • Key Takeaway/Example: Add links like "Add Movie" to /add-edit-movie. Use data-bs-theme="dark" for styling.
  • Link for More Details: Ask AI: Bootstrap Navbar in Blazor

Implementing Add and Edit Operations

  • Summary: Create AddEdit.razor component for both add (/add-edit-movie) and edit (/add-edit-movie/{id}). Use EditForm with binding and validation from data annotations. Fetch genres for dropdown. Handle submit to call API POST or PUT.
  • Key Takeaway/Example: Differentiate add/edit via route parameter. Use floating labels and input types (text, select, number, date).
<EditForm Model="@movie" OnValidSubmit="HandleSubmitAsync" FormName="add-edit-movie">
    <ValidationSummary />
    <!-- Form fields -->
    <button type="submit" class="btn btn-primary">Save</button>
    <button type="button" class="btn btn-secondary" @onclick="() => navigationManager.NavigateTo("/")">Cancel</button>
</EditForm>

Implementing Delete Operation with Modal

  • Summary: Add DeleteMovie.razor as child component in Home.razor, passing movie parameter. Use Bootstrap modal for confirmation, triggered by button with dynamic ID. On confirm, call API DELETE and navigate home.
  • Key Takeaway/Example: Generate unique modal IDs like "delete-modal-{movie.Id}". Wire @onclick to async delete method.
<div class="modal fade" id="@DeleteMovie.GetModalId(movie)" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Delete Movie: @movie.Name</h5>
            </div>
            <!-- Footer with buttons -->
        </div>
    </div>
</div>

Conclusion and Final Demo

  • Summary: The app now supports full CRUD: reading movies in cards, adding/editing via forms, and deleting with modal confirmation. Data persists in SQL Server localDB.
  • Key Takeaway/Example: Troubleshoot issues like hot reload failures or null references by rebooting and checking dependencies. The decoupled setup allows focused development on frontend or backend.
  • Link for More Details: Ask AI: ASP.NET Core CRUD Demo

About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: