Blazor in Action

Book description

An example-driven guide to building reusable UI components and web frontends—all with Blazor, C#, and .NET.

In Blazor in Action, you will learn about:

  • Blazor + WebAssembly
  • Picking the right hosting model
  • Building reusable UI components
  • Building forms with validation
  • Integrating with JavaScript libraries
  • Securing your application
  • Testing your applications

Blazor in Action is a practical guide to building stunning UIs and client-side applications using C# and .NET. You’ll use the Blazor frontend framework to create a fun and exciting web application for plotting hiking routes. As you build up your new application, you’ll master the key features of Blazor, such as routing, forms and validation, and dynamic and reusable components. By the time you're done, you'll be ready to develop beautiful sites and apps that seamlessly execute your C# code natively in the browser.

The book is written to the most recent stable build of Blazor and seamlessly integrates fresh features from .NET 6.

About the Technology
Create rich web frontends without relying on JavaScript. Microsoft’s Blazor framework uses WebAssembly to extend the ultra-popular ASP.NET platform. In Blazor, you can build interactive web components that run natively in the browser without plug-ins or transpilers. And because it’s C# end-to-end, it’s easy to share code between the server and your web UI.

About the Book
Blazor in Action teaches you to create full-stack ASP.NET applications end-to-end in C#. You’ll start by learning to build Blazor web components, working through core topics like routing and forms. As you go, you’ll implement a hiking route web application that includes reusable code, integration with JavaScript libraries, and role-based security. To make sure your app is production ready, this practical book also covers state management, data persistence, and testing.

What's Inside
  • Dynamic and reusable UI components
  • Sharing client and server code
  • Role-based security using Auth0
  • Persisting state using local browser storage


    • About the Reader
      For web developers with C# and .NET experience.

      About the Author
      Chris Sainty has been a part of the Blazor community from the beginning. He’s an active blogger, open source developer, international speaker, and a Microsoft MVP.

      Quotes
      The right place to start your journey with Blazor.
      - Kalyan Chanumolu, Microsoft

      An excellent introduction and reference for Blazor development.
      - Jeff Smith, TJX

      Guides the user through the practical use of Blazor components and the concepts behind them. Fantastic book!
      - Jim Wilson, Open Applications Group

      A very well-conceived and well-executed introduction to Blazor and its use in full-stack development.
      - Mark Chalkley, MainStreetCities

Table of contents

  1. inside front cover
  2. Blazor in Action
  3. Copyright
  4. dedication
  5. contents
  6. front matter
    1. preface
    2. acknowledgments
    3. about this book
      1. Who should read this book
      2. How this book is organized: A road map
      3. About the code
      4. liveBook discussion forum
    4. about the author
    5. about the cover illustration
  7. 1 Starting your Blazor journey
    1. 1.1 Why choose Blazor for new applications?
    2. 1.2 Components, a better way to build UI
      1. 1.2.1 What is a component?
      2. 1.2.2 The benefits of a component-based UI
      3. 1.2.3 Anatomy of a Blazor component
    3. 1.3 Blazor, a platform for building modern UIs with C#
      1. 1.3.1 Understanding hosting models
      2. 1.3.2 Blazor WebAssembly
      3. 1.3.3 Blazor Server
      4. 1.3.4 Other hosting models
    4. Summary
  8. 2 Your first Blazor app
    1. 2.1 Setting up the application
      1. 2.1.1 Blazor WebAssembly template configurations
      2. 2.1.2 Creating the application
    2. 2.2 Building and running the application
    3. 2.3 Key components of a Blazor application
      1. 2.3.1 Index.html
      2. 2.3.2 Program.cs
      3. 2.3.3 App.razor
      4. 2.3.4 wwwroot folder and _Imports.razor
    4. 2.4 Writing your first components
      1. 2.4.1 Organizing files using feature folders
      2. 2.4.2 Setting up styling
      3. 2.4.3 Defining the layout
      4. 2.4.4 The Blazing Trails home page
    5. Summary
  9. 3 Working with Blazor’s component model
    1. 3.1 Structuring components
      1. 3.1.1 Single file
      2. 3.1.2 Partial class
    2. 3.2 Component life cycle methods
      1. 3.2.1 The first render
      2. 3.2.2 The life cycle with async
      3. 3.2.3 Dispose: The extra life cycle method
    3. 3.3 Working with parent and child components
      1. 3.3.1 Passing values from a parent to a child
      2. 3.3.2 Passing data from a child to a parent
    4. 3.4 Styling components
      1. 3.4.1 Global styling
      2. 3.4.2 Scoped styling
      3. 3.4.3 Using CSS preprocessors
    5. Summary
  10. 4 Routing
    1. 4.1 Introducing client-side routing
      1. 4.1.1 Blazor’s router
      2. 4.1.2 Defining page components
    2. 4.2 Navigating between pages programmatically
    3. 4.3 Passing data between pages using route parameters
    4. 4.4 Handling multiple routes with a single component
    5. 4.5 Working with query strings
      1. 4.5.1 Setting query-string values
      2. 4.5.2 Retrieving query-string values using SupplyParameterFromQuery
    6. Summary
  11. 5 Forms and validation—Part 1: Fundamentals
    1. 5.1 Super-charging forms with components
      1. 5.1.1 Creating the model
      2. 5.1.2 Basic EditForm configuration
      3. 5.1.3 Collecting data with input components
      4. 5.1.4 Creating inputs on demand
    2. 5.2 Validating the model
      1. 5.2.1 Configuring validation rules with Fluent Validation
      2. 5.2.2 Configuring Blazor to use Fluent Validation
    3. 5.3 Submitting data to the server
      1. 5.3.1 Adding MediatR to the Blazor project
      2. 5.3.2 Creating a request and handler to post the form data to the API
      3. 5.3.3 Setting up the endpoint
    4. Summary
  12. 6 Forms and validation—Part 2: Beyond the basics
    1. 6.1 Customizing validation CSS classes
      1. 6.1.1 Creating a FieldCssClassProvider
      2. 6.1.2 Using custom FieldCssClassProviders with EditForm
    2. 6.2 Building custom input components with InputBase
      1. 6.2.1 Inheriting from InputBase<T>
      2. 6.2.2 Styling the custom component
      3. 6.2.3 Using the custom input component
    3. 6.3 Working with files
      1. 6.3.1 Configuring the InputFile component
      2. 6.3.2 Uploading files when the form is submitted
    4. 6.4 Updating the form to allow editing
      1. 6.4.1 Separating the trail form into a standalone component
      2. 6.4.2 Refactoring AddTrailPage.razor
      3. 6.4.3 Adding the edit trail feature
      4. 6.4.4 Testing the edit functionality
    5. Summary
  13. 7 Creating more reusable components
    1. 7.1 Defining templates
    2. 7.2 Enhancing templates with generics
    3. 7.3 Sharing components with Razor class libraries
    4. Summary
  14. 8 Integrating with JavaScript libraries
    1. 8.1 Creating a JavaScript module and accessing it via a component
      1. 8.1.1 Testing out the RouteMap component
      2. 8.1.2 Calling JavaScript functions from C# and returning a response
    2. 8.2 Calling C# methods from JavaScript
    3. 8.3 Integrating the RouteMap component with the TrailForm
    4. 8.4 Displaying the RouteMap on the TrailDetails drawer
    5. Summary
  15. 9 Securing Blazor applications
    1. 9.1 Integrating with an identity provider: Auth0
      1. 9.1.1 Registering applications with Auth0
      2. 9.1.2 Customizing tokens from Auth0
      3. 9.1.3 Configuring Blazor WebAssembly to use Auth0
      4. 9.1.4 Configuring ASP.NET Core WebAPI to use Auth0
    2. 9.2 Displaying different UI fragments based on authentication status
      1. 9.2.1 Updating the Home feature
    3. 9.3 Prevent unauthorized users accessing a page
      1. 9.3.1 Securing API endpoints
      2. 9.3.2 Calling secure API endpoints from Blazor
    4. 9.4 Authorizing users by role
      1. 9.4.1 Adding roles in Auth0
      2. 9.4.2 Consuming Auth0 roles in Blazor WebAssembly
      3. 9.4.3 Implementing role-based logic
    5. Summary
  16. 10 Managing state
    1. 10.1 Simple state management using an in-memory store
      1. 10.1.1 Creating and registering a state store
      2. 10.1.2 Saving data entered on the form to AppState
    2. 10.2 Improving the AppState design to handle more state
    3. 10.3 Creating persistent state with browser local storage
      1. 10.3.1 Defining an additional state store
      2. 10.3.2 Adding and removing trails from thefavorites list
      3. 10.3.3 Displaying the current number of favorite trails
      4. 10.3.4 Reorganizing and refactoring
      5. 10.3.5 Showing favorited trails on the favorite trails page
      6. 10.3.6 Initializing AppState
    4. Summary
  17. 11 Testing your Blazor application
    1. 11.1 Introducing bUnit
    2. 11.2 Adding a bUnit test project
    3. 11.3 Testing components with bUnit
      1. 11.3.1 Testing rendered markup
      2. 11.3.2 Triggering event handlers
      3. 11.3.3 Faking authentication and authorization
      4. 11.3.4 Emulating JavaScript interactions
      5. 11.3.5 Testing multiple components
    4. Summary
  18. Appendix A. Adding an ASP.NET Core backend to a Blazor WebAssembly app
    1. A.1 Adding an ASP.NET Core Web API
      1. A.1.1 Removing boilerplate from the new API project
      2. A.1.2 Configuring the API
    2. A.2 Adding a .NET class library to share code between client and API
    3. A.3 Setting up an SQLite database in the API
      1. A.3.1 Configuring the initial entities for the system
      2. A.3.2 Setting up the database context
      3. A.3.3 Connection strings and service configuration
      4. A.3.4 Creating the first migration and creating the database
  19. Appendix B. Updating existing areas to use the API
    1. B.1 Creating a new API endpoint that returns all trails
    2. B.2 Updating the Home feature to load trail data from the API
      1. B.2.1 Adding the GetTrailsHandler class
      2. B.2.2 Updating HomePage.razor and SearchPage.razor to use GetTrailRequest via MediatR
      3. B.2.3 Updating TrailCard.razor and TrailDetails.razor
  20. index

Product information

  • Title: Blazor in Action
  • Author(s): Chris Sainty
  • Release date: June 2022
  • Publisher(s): Manning Publications
  • ISBN: 9781617298646