Quickstarters

How to Create a Basic CRUD Application with Blazor?

40min

Overview

This guide will walk you through building a complete CRUD (create, read, update, delete) application using Blazor.

In this tutorial, you'll configure a Back4app project, design your database schema, and integrate your Blazor frontend with Back4app via REST APIs.

This process will help you establish a robust backend while ensuring your web application can efficiently manage data.

Initially, you'll set up a Back4app project titled Basic-CRUD-App-Blazor, which will serve as the backbone of your data management system.

Next, you'll craft a scalable database design by establishing detailed collections and fields, either manually or with the assistance of the Back4app AI Agent.

After that, you'll harness the power of the Back4app Admin App—a user-friendly, drag and drop interface—to manage your collections effortlessly.

Finally, you will connect your Blazor frontend to Back4app using REST APIs and secure your backend with proper access control.

By the end of this tutorial, you will have a production-ready web application that supports essential CRUD operations along with user authentication and secure data updates.

Key Learning Points

  • Build CRUD applications that handle data operations seamlessly using a reliable backend.
  • Acquire insights into creating a scalable backend and integrating it with a Blazor frontend.
  • Utilize the drag and drop capabilities of the Back4app Admin App to simplify data management.
  • Learn deployment techniques, including Docker containerization, to rapidly launch your application.


Prerequisites

Before starting, make sure you have:

  • A Back4app account and a new project set up. Visit Getting Started with Back4app if you need assistance.
  • A Blazor development environment. Use Visual Studio or Visual Studio Code with the latest .NET SDK (version 6 or above).
  • Basic knowledge of C#, Blazor, and RESTful APIs. Check out the Blazor documentation if required.


Step 1 – Initial Project Setup

Establishing a New Back4app Project

  1. Sign into your Back4app account.
  2. Select the “New App” button from your dashboard.
  3. Input the project title: Basic-CRUD-App-Blazor and follow the on-screen instructions to complete the creation process.
Create New Project
Create New Project


After setup, your new project will display on your dashboard, providing a sturdy foundation for configuring your backend.



Step 2 – Designing Your Database Schema

Crafting Your Data Model

For this basic CRUD application, you'll need to create several collections. Below are examples of the collections along with the required fields and data types to set up your database efficiently.

1. Items Collection

Field

Data Type

Purpose

_id

ObjectId

Auto-generated unique identifier.

title

String

The name or title of the item.

description

String

A short summary of the item.

created_at

Date

The timestamp when the item was created.

updated_at

Date

The timestamp when the item was last modified.

2. Users Collection

Field

Data Type

Purpose

_id

ObjectId

Auto-generated unique identifier.

username

String

Unique identifier for the user.

email

String

User’s email address.

password_hash

String

Encrypted password for secure login.

created_at

Date

Timestamp of account creation.

updated_at

Date

Timestamp of the last account update.

You can create these collections manually in the Back4app dashboard by establishing a new class for each collection and adding the necessary columns.

Create New Class
Create New Class


You can define columns by choosing a data type, naming the field, setting default values, and determining if the field is mandatory.

Create Column
Create Column


Employing the Back4app AI Agent for Schema Creation

The Back4app AI Agent streamlines schema creation directly from your dashboard. By entering a prompt detailing your collections and fields, the AI Agent automatically generates the necessary database schema.

How to Utilize the AI Agent:

  1. Access the AI Agent: Log in to your Back4app dashboard and locate the AI Agent in the settings or main menu.
  2. Define Your Data Model: Paste a descriptive prompt that lists the collections and their fields.
  3. Review and Confirm: Check the suggested schema and apply it to your project.

Sample Prompt

Text


Using this AI Agent not only saves time but also ensures a consistent and efficient schema setup.



Step 3 – Activating the Admin App & Managing CRUD Operations

Introduction to the Admin App

The Back4app Admin App provides a no-code, drag and drop interface to manage your backend data. This intuitive tool enables you to perform CRUD operations with ease.

Activating the Admin App

  1. Navigate to the “More” menu on your Back4app dashboard.
  2. Click on “Admin App” and then select “Enable Admin App.”
  3. Set up your admin credentials by creating an initial admin user, which will also establish default roles and system collections.
Enable Admin App
Enable Admin App


After enabling, log in to the Admin App to manage your collections and data.

Admin App Dashboard
Admin App Dashboard


Performing CRUD Operations with the Admin App

Inside the Admin App you can:

  • Create Records: Click the “Add Record” button within a collection (for example, Items) to add new entries.
  • Read/Update Records: Click on a record to inspect its details or modify its fields.
  • Delete Records: Remove entries that are no longer necessary.

This simple, drag and drop interface significantly enhances the data management experience.



Step 4 – Connecting Blazor with Back4app

Now that your backend is configured, it’s time to integrate your Blazor frontend with Back4app.

Using REST APIs in Blazor

Since the Parse SDK is not applicable to Blazor, you will leverage REST APIs to perform CRUD operations. In your Blazor application, use the HttpClient to interact with Back4app’s REST endpoints.

Example: Fetching Items via REST

Create a service in your Blazor project to fetch items. For example:

C#


Integrate such API calls within your Blazor components to manage CRUD operations.



Step 5 – Securing Your Backend

Implementing Access Control Lists (ACLs)

Protect your data by setting ACLs on your objects. For instance, to create a private item:

C#


Configuring Class-Level Permissions (CLPs)

Within the Back4app dashboard, adjust the CLPs for each collection to ensure only authorized users can access or modify sensitive data.



Step 6 – Managing User Authentication

Setting Up User Accounts in Blazor

Back4app utilizes Parse’s User class to handle authentication. In your Blazor app, manage user sign-up and sign-in via REST calls. For example:

C#


Similar methods can be implemented for login and session management. Additional functionalities like social logins, email verification, and password recovery can be set up via the Back4app dashboard.



Step 7 – Deploying Your Blazor Frontend

Back4app’s Web Deployment feature allows you to seamlessly host your Blazor application by deploying your code from a GitHub repository.

7.1 Creating Your Production Build

  1. Open your project directory in a terminal.
  2. Run the publish command:

    Bash
    

    This command generates a published folder with optimized static files for your Blazor app.

  3. Verify the Build: Confirm that the output folder contains the necessary files (e.g., index.html, JavaScript, CSS, etc.).


7.2 Organizing and Uploading Your Code

Your Git repository should encapsulate the complete source of your Blazor application. An example folder structure might be:

Text


Sample File: Program.cs

C#


Commit and Push to GitHub

  1. Initialize Git in your project folder (if not already done):

    Bash
    
  2. Add your files:

    Bash
    
  3. Commit your changes:

    Bash
    
  4. Create a GitHub repository: Name it Basic-CRUD-App-Blazor-Frontend.
  5. Push your repository:

    Bash
    


7.3 Integrating Your Repository with Web Deployment

  1. Access the Web Deployment option: In your Back4app dashboard, open your project (Basic-CRUD-App-Blazor) and navigate to the Web Deployment section.
  2. Connect Your GitHub Account: Follow the prompts to link your GitHub account, allowing Back4app to access your repository.
  3. Select the Repository and Branch: Choose the repository (e.g., Basic-CRUD-App-Blazor-Frontend) and the branch (e.g., main) that holds your Blazor code.


7.4 Deployment Configuration

Provide additional details such as:

  • Build Command: If the published folder isn’t pre-generated, specify a command like dotnet publish -c Release.
  • Output Directory: Set the output path (for example, bin/Release/net6.0/wwwroot).
  • Environment Variables: Add any required environment settings (such as API keys) in the configuration.


7.5 Containerizing Your Blazor Application with Docker

If you prefer Docker for deployment, include a Dockerfile in your repository. Here’s an example:

Dockerfile


Then, in your Web Deployment settings, select the Docker option to deploy your containerized application.



7.6 Launching Your Application

  1. Initiate the Deployment: Click the Deploy button in the Back4app dashboard.
  2. Monitor the Build: Back4app will fetch your code from GitHub, run the build command if necessary, and deploy your app in a container.
  3. Get Your URL: Once the deployment process is complete, Back4app will supply a URL for your hosted Blazor application.


7.7 Confirming the Deployment

  1. Open the Provided URL: Visit the URL in a browser to view your deployed app.
  2. Test the Application: Verify that all pages load correctly and that CRUD operations function as expected.
  3. Troubleshoot if Necessary: Use browser developer tools and review deployment logs on Back4app to diagnose any issues.


Step 8 – Summary and Further Exploration

Congratulations! You have successfully built a basic CRUD application using Blazor and Back4app.

You set up a project titled Basic-CRUD-App-Blazor, designed collections for Items and Users, and utilized the Back4app Admin App for seamless data management.

Additionally, you integrated your Blazor frontend with Back4app using REST APIs and applied robust security measures.

Future Steps:

  • Expand Your Frontend: Enhance your Blazor app with more detailed views, search functionality, or live notifications.
  • Add Advanced Features: Consider incorporating Cloud Functions, third-party integrations, or role-based permissions.
  • Consult Additional Resources: Review the Back4app documentation and other advanced tutorials to further optimize and expand your application.

By following this guide, you now have the foundation to build a scalable and secure CRUD backend for your Blazor application using Back4app. Happy coding!