September 22nd, 2020

Azure Static Web Apps with .NET and Blazor

Aaron Powell
Principal Cloud Advocate

Azure Static Web Apps is a service that automatically builds and deploys full stack web apps from a GitHub repository. Azure Static Web Apps consist of a static web frontend, and an Azure Functions based backend. When you create a Static Web Apps resource, Azure sets up a GitHub Actions workflow in the app’s source code repository that monitors a branch of your choice. Every time you push commits to the watched branch, the GitHub Action automatically builds and deploys your app and its API.

As of today, Azure Static Web Apps now has first-class support for Blazor WebAssembly and .NET Functions in preview, available in all supported regions. This was one of the top user requests since Static Web Apps was announced at Build. You can develop and deploy a frontend and a serverless API written entirely in .NET.

Let’s give it a try!

Getting Started

To help you get started, we’ve created a GitHub repository template, which you can use as a starting point for your own projects.

In GitHub, click on the Use this template button to create a new GitHub repository using the template, providing it a name of your choosing (we’ll use my-blazor-app here) and click Create repository from template.

There are three folders in the template:

  • Client: The Blazor WebAssembly sample application
  • API: A C# Azure Functions API, which the Blazor application will call
  • Shared: A C# class library with a shared data model between the Blazor and Functions application

To run the app locally, start both the API and Client projects. The “Fetch data” page in the Blazor app requests weather forecast data form the backend Function API and display it on the page.

To run the app from the command-line, you’ll need to install the Azure Functions Core Tools for your platform of choice.

Deploying to Static Web Apps

To deploy this app as an Azure Static Web App, log into your Azure account (sign up for a free one here) and search for Static Web Apps.

Static Web Apps in the portal

Click Create, provide a Subscription, Resource Group and name for the application.

Next, sign into GitHub and locate your GitHub repo (my-blazor-app) and select the branch you wish to deploy.

Finally, select Blazor from the Build Presets, which will populate the App location, API location and App artifact location with Client, API and wwwroot. The first and second values are the path within the Git repository to where the project files for the Blazor and Functions app resides, so if you’ve modified the structure of the Git repository, make sure these values are updated to reflect. The third value is the output path that Blazor will compile into and doesn’t need updating.

Azure Static Web App wizard

Complete the wizard and Static Web Apps will create the GitHub Actions workflow file for you and deploy your application to Azure.

gif of Action completing and then going to the portal to open the deployed app

Summary

We started by forking a GitHub repository (but you could’ve also used a project created in Visual Studio/Visual Studio Code/etc.), created a Static Web App in Azure, and it setup a GitHub Actions workflow for us, automatically building and deploying our Blazor and Functions application. We’ve now got a statically hosted application with a serverless backend, ready to scale for our demands.

Additional Resources

To learn more about Blazor WebAssembly and .NET Functions on Static Web Apps, check out the quickstart docs, the Microsoft Learn module, as well as the comprehensive Static Web Apps docs.

Give Feedback

We’re excited to have Blazor WebAssembly and .NET Functions supported by Azure Static Web Apps, and can’t wait to see what you build with them. Let us know what you think by filing an issue on GitHub.

Author

Aaron Powell
Principal Cloud Advocate

Aaron is a Developer Advocate at Microsoft. Having spent 15 years doing web development he’s seen it all, from browser wars, the rise of AJAX and the fall of 20 JavaScript frameworks (and that was just yesterday!). Always tinkering with something new he explores crazy ideas like writing your own implementation of numbers in .NET, creating IoC in JavaScript or implementing tic-tac-toe using git commits.

14 comments

Discussion is closed. Login to edit/delete existing comments.

  • Zeid Shubailat

    Thanks… works nicely.

    Where are the compiled files stored? i created a .gitignore and removed bin and obj. Trying to understand where the compiled files will be located

  • mscotover

    Hi Team,

    Happy Friday!
    Quick question: Does Azure Static Web Apps support to install a certificate for a custom domain ? Let’s say I have a custom domain http://www.mydomain1.com, and I want to install an SSL certificate to access the Static Web App at https://www.mydomain1.com. Is that possible ? Thank you!

    • mscotover

      Hi Team,

      Nevermind. I found out that I can use Azure Content Delivery Network (CDN) to accomplish this goal.
      Thank you though!

  • Orion Adrian

    Do we have an issue tagged for adding function monitoring and diagnostics. There’s really not much there and it makes it very hard to tell why things aren’t working.

  • Péter Vincze

    Will there be Azure Devops integration, or just Github?

  • Jan Fruseth

    Can someone please give sample how to use Microsoft DevOps instead of Github, please.

  • Preishuber

    as of my today experience publishing from Visual Studio doenst work for 5 RC1
    Dialog pops up 3.18 max or self hosted.

  • Ben Hayat

    To use Azure functions as our Web API backend on Azure, how do we then connect and use Azure PosgreSQL via and ORM like EF.Net core?
    I hope you’re not suggesting to go back to old ADO.Net to write SQL statements in Azure functions?

      • Ben Hayat

        You can use Entity Framework Core from Azure Functions

        Oh wow, I didn’t know that…
        Thank you Dan for answering my question

    • Jorge Morales Vidal

      You can use Entity Framework Core with Azure Functions, as described by Jeff Hollan in this article: https://dev.to/azure/using-entity-framework-with-azure-functions-50aa
      You can find the PostgreSQL driver for EF Core documentation in the Npgsql website: https://www.npgsql.org/efcore/index.html, as far as I can see it behaves in the same way as the SQL Server driver for EF Core, so it should work with Azure Database for PostgreSQL, as mentioned in the Microsoft docs: https://docs.microsoft.com/en-us/dotnet/api/overview/azure/postgresql?view=azure-dotnet

      Read more