Get started with Blazor

By Daniel Roth and Luke Latham

In a few steps, get started with Blazor:

  1. Install the latest .NET Core 3.0 Preview SDK release.

  2. Install the Blazor templates by running the following command in a command shell:

    dotnet new -i Microsoft.AspNetCore.Blazor.Templates::3.0.0-preview4-19216-03
    
  3. Follow the guidance for your choice of tooling:

    1. Install the latest preview of Visual Studio 2019 with the ASP.NET and web development workload.
    2. Install the latest Blazor extension from the Visual Studio Marketplace. This step makes Blazor templates available to Visual Studio.
    3. Enable Visual Studio to use preview SDKs:
      1. Open Tools > Options in the menu bar.
      2. Open the Projects and Solutions node. Open the .NET Core tab.
      3. Check the box for Use previews of the .NET Core SDK. Select OK.
    4. Create a new project.
    5. Select ASP.NET Core Web Application. Select Next.
    6. Provide a name in the Project name field. Confirm the Location entry is correct or provide a location for the project. Select Create.
    7. Make sure .NET Core and ASP.NET Core 3.0 are selected at the top.
    8. For an experience with Blazor server-side, choose the Razor Components template. The template will be renamed "Blazor (server-side)" in a future preview. For an experience with Blazor client-side, choose the Blazor template. Select Create.
    9. Press F5 to run the app.

In a browser, navigate to https://localhost:5001.

Multiple pages are available from tabs in the sidebar:

  • Home
  • Counter
  • Fetch data

On the Counter page, select the Click me button to increment the counter without a page refresh. Incrementing a counter in a webpage normally requires writing JavaScript, but Razor components provide a better approach using C#.

Pages/Counter.razor:

@page "/counter"

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" onclick="@IncrementCount">Click me</button>

@functions {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
}

A request for /counter in the browser, as specified by the @page directive at the top, causes the Counter component to render its content. Components render into an in-memory representation of the render tree that can then be used to update the UI in a flexible and efficient way.

Each time the Click me button is selected:

  • The onclick event is fired.
  • The IncrementCount method is called.
  • The currentCount is incremented.
  • The component is rendered again.

The runtime compares the new content to the previous content and only applies the changed content to the Document Object Model (DOM).

Add a component to another component using an HTML-like syntax. Component parameters are specified using attributes or child content. For example, a Counter component can be added to the app's homepage by adding a <Counter /> element to the Index component.

Pages/Index.razor:

@page "/"

<h1>Hello, world!</h1>

Welcome to your new app.

<Counter />

Run the app. The homepage has its own counter.

To add a parameter to the Counter component, update the component's @functions block:

  • Add a property for IncrementAmount decorated with the [Parameter] attribute.
  • Change the IncrementCount method to use the IncrementAmount when increasing the value of currentCount.

Pages/Counter.razor:

@functions {
    private int currentCount = 0;

    [Parameter]
    private int IncrementAmount { get; set; } = 1;

    private void IncrementCount()
    {
        currentCount += IncrementAmount;
    }
}

Specify an IncrementAmount parameter in the Home component's <Counter> element using an attribute.

Pages/Index.razor:

<Counter IncrementAmount="10" />

Run the app. The homepage has its own counter that increments by ten each time the Click me button is selected.

Next steps

Build your first Blazor app