Create a Blazor Server project
🟥 Not applicable to Blazor WebAssembly
In this tutorial, we will guide you through the steps of setting up and constructing a basic Blazor Server application from the ground up. To accomplish this, we will leverage Visual Studio, a well-known .NET development environment, for creating and building our project. Specifically, we will cover the following topics:
- Preparing your local machine for development.
- Choosing appropriate project templates and configuration options.
Preparing your local machine for development
To create a Blazor Server project, you need:
Once you installed Visual Studio 2022, run Visual Studio Installer and click Modify.
Tick ASP.NET and web development.
Switch to tab Individual components and select .NET 7 Runtime.
Choosing appropriate project templates and configuration options
When creating a new project in Visual Studio, you will be presented with a variety of templates to choose from, including official templates provided by Microsoft and third-party templates for Blazor Server.
The two official templates provided by Microsoft for Blazor Server are:
- Blazor Server App: This template provides a pre-built navigation bar and Bootstrap 5, which can be useful for quickly getting started with a new project.
- Blazor Server App Empty: This template provides a blank slate, with no pre-built code or libraries. This can be useful if you prefer to build your own navigation bar or use a different CSS framework.
When selecting a template, keep in mind that using a third-party template might prevent you from debugging the project. Follow the following steps to create a new project:
- Start your Visual Studio, select Create a new project.
- A list of templates will appear, you need to select one of the template: Blazor Server App or Blazor Server App Empty.
- Select the options for your project.
After selecting a template, you will have the option to configure your project with the following options:
- Framework: Allows you to choose the target framework of the project.
- Authentication type (only available for Blazor Server App template): Allows you to generate code for authentication. It is recommended to leave it as None and build the authentication functionality yourself as the generated code may not meet all your needs for a basic project.
- Configure for HTTPS: Allows you to add a profile named https and it will redirect HTTP to HTTPS, which is recommended for security in production.
- Enable Docker: Generates a Dockerfile which allow you to package and deploy your application as a Docker container.
- Do not use top-level statements (only available for Blazor Server App template): Allows you to use the old syntax for the project, it is recommended to not use this option to adapt the latest syntax of C#.
It is important to keep in mind that many options can be implemented manually at a later stage in a project, and it is not necessary to stress about selecting them at the beginning. It is often more effective to start with a minimal set of options and gradually add more as needed. This allows for flexibility and adaptability throughout the project.