How to Build a Blazing-Fast Blazor Blog with Cosmic
Tags: blazor, cosmicjs, webdev, dotnet
Published: 2020-10-25 10:44:57

In this tutorial, I'm going to show you how to create a simple but blazing fast blog using [Blazor WebAssembly](https://dotnet.microsoft.com/apps/aspnet/web-apps/blazor),  [Tailwind CSS](https://tailwindcss.com/), and [Cosmic](https://www.cosmicjs.com/). Without further ado, grab some coffee & let's build a cool app!

# TL;DR
* [Demo](https://www.cosmicjs.com/apps/blazor-cosmic-blog)
* [Download Source Code](https://github.com/cosmicjs/blazor-blog)

# Prerequisites
* Installed the latest  [.NET Core SDK](https://dotnet.microsoft.com/download). 
* Install the Blazor project templates
* Cosmic account
* Basic knowledge of Blazor

Let's get started with our technology stack. 

# What is Blazor?
Blazor is a .NET framework developed and maintained by Microsoft. It's been picking up steam over the last couple of years, and recently attracted quite a bit of attention in Spring of 2020 with the launch of Blazor WebAssembly - which is what we'll be using today. If you want to build an interactive web application using C# then you should definitely check Blazor.

According to Blazor's [docs](https://dotnet.microsoft.com/apps/aspnet/web-apps/blazor):

> Blazor is a free and open-source web framework that enables developers to create web apps using C# and   HTML. It is being developed by Microsoft. Blazor lets you build interactive web UIs using C# instead of JavaScript. Blazor apps are composed of reusable web UI components implemented using C#, HTML, and CSS. Both client and server code is written in C#, allowing you to share code and libraries. Blazor can run your client-side C# code directly in the browser, using WebAssembly. Because it's real .NET running on WebAssembly, you can re-use code and libraries from server-side parts of your application.

There are two ways to create Blazor apps:

* Blazor Server
* Blazor WebAssembly

So in this article, we are going to use Blazor WebAssembly - which is one way to host Blazor components client-side in the browser using a WebAssembly-based .NET runtime.

# What about Cosmic?
Cosmic is a headless CMS that will enable us to build our application surprisingly quickly and deploy it to Netlifly in moments. Unlike traditional CMS, Cosmic offers us as the developers a lot of flexibility to develop this application how we please and take it to whatever hosting platform we wish. It is also very easy to store and retrieve data, which makes it perfect for building apps like this.

# Creating Your Blog Bucket in Cosmic
To create a blog app we will require data. To manage and manipulate this data, we will be using the power of Cosmic buckets and objects. To get started:

1.  Create a [free account on Cosmic](https://app.cosmicjs.com/signup). If this is your first time using Cosmic, you'll see a short tutorial which will introduce some of the data types we'll be using - buckets, objects, and metafields.

1. Create a new empty bucket & name it blazor-blog. For each blog post, we will have three object types:

* Title
* Content
* Image

So, go ahead and add these properties in your buckets along with some data for those objects. [If you're having trouble with Buckets, Objects, or Metafields, go ahead and read the Getting Started page really quickly for some startup instructions](https://www.cosmicjs.com/getting-started), then come on back to this article.


# Creating Your Blazor WebAssembly app
You can now create and run your first Blazor WebAssembly app by running:

```
dotnet new blazorwasm -o BlazorCosmicBlog --pwa
cd BlazorCosmicBlog
dotnet run
```
Now open the https://localhost:44346/  to see the default blazor app.

![new-app](https://dev-to-uploads.s3.amazonaws.com/i/nlzfl9h4kccgrzs2yeqp.PNG)

To fetch blog posts saved in Cosmic we require a Cosmic bucket Slug. Go to Cosmic -> Bucket -> Settings -> Basic Settings and copy the **Bucket Slug**.

![api](https://cdn.cosmicjs.com/476e8050-fa60-11ea-ac76-ed0a0ac0571c-bucket-slug.PNG)

To store the slug, create a new file **appsettings.josn** in the **wwwroot** folder and add the below code:

{% gist https://gist.github.com/sumitkharche/20d690f99db80fbbcfa9113cedbf0920 %}

Now open the program.cs file and add the below code to load the appsettings.json file.

{% gist https://gist.github.com/sumitkharche/eae8269167f38930ad174d322fece6b7 %}

We are using Tailwind CSS to style our application, so let's add CDN into the index.html file.

```css
<link https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
```
# Building Our Blog Pages
First, we are building the main category page where we can show all the blog posts. Create a folder called Models and add class to store posts.

{% gist https://gist.github.com/sumitkharche/0bc83a90589f4acabd36d00d83d9cfe3 %}

Go to **Pages** folder and open **Index.razor** component and add the below code to show list of posts fetching from Cosmic.

{% gist https://gist.github.com/sumitkharche/beea9389a8496a9dde2ff6ada3658102 %}

Let's run the app to see the output.

![index](https://cdn.cosmicjs.com/8eaabce0-fa60-11ea-ac76-ed0a0ac0571c-all-posts---Copy.PNG)

Looking good! Create a new component into the Pages folder called **PostDetails.razor** which shows the details of posts when we click on **Read More** button from all post pages.

{% gist https://gist.github.com/sumitkharche/43f962509b4db8a9c319a61ff40d91ee %}

Now run the app and click on Read More button from the Home page to see post details.

![post-details](https://cdn.cosmicjs.com/7a897830-fa62-11ea-ac76-ed0a0ac0571c-single-post-new---Copy.PNG)

That's it! All our core features are working. We have successfully created a blog application using Blazor and Cosmic. Now it's just up to your content team members to add some blog posts to it.

# Deploying Your Blazor Blog to Hosting Platforms

We can now deploy our application on any hosting platform. We are deploying our app on [Netlify](https://www.netlify.com/) using Github Actions. 

1. Commit your code to the GitHub repository.
1. Login to Netlify and create a new site. 
1. We need a Personal Access Token and Site Id to deploy our app to netlify. So Go to Profile and generate Personal Access Token.

![netlify1](https://cdn.cosmicjs.com/e37c4ff0-fa64-11ea-ac76-ed0a0ac0571c-access-token.PNG)

4.  Go to your site -> Site details
![netlify2](https://cdn.cosmicjs.com/7eaf4cc0-fa65-11ea-ac76-ed0a0ac0571c-site-details.PNG)

5. Go to your repository Settings -> Secrets and above two secrets:
![netlify3](https://cdn.cosmicjs.com/699aadc0-fa6f-11ea-ac76-ed0a0ac0571c-github-secrets.PNG)

6. Click on Actions -> New Workflow and select .NET Core template. After that add the below code to yml file.

7. {% gist https://gist.github.com/sumitkharche/514f44933419cbf4f1207530ba95a05d %}

8. Our blog app is successfully deployed to Netlify.
![netlify4](https://cdn.cosmicjs.com/a5ad8690-fa72-11ea-ac76-ed0a0ac0571c-ezgif.com-gif-maker.gif)

# Conclusion

In this article, I have demonstrated you how to create a blogging application using Blazor and Cosmic. The Cosmic Community has a lot of examples on how to handle integration with email functions, download functions, and third-party platforms. If you're interested in building your own applications like this, [get started by setting up your free account](https://app.cosmicjs.com/signup).

I really hope that you enjoyed this little app, and please do not hesitate to send me your thoughts or comments about what could I have done better.

If you have any comments or questions about building apps with Cosmic, [reach out to us on Twitter](https://twitter.com/cosmicjs) and [join the conversation on Slack](https://www.cosmicjs.com/community).