Fun with Dad Jokes



I was browsing Javascript projects that are available online so I can convert them to  Blazor.  It is fun to try to do everything they did in JavaScript in Blazor.  I came across this application.   This project was created just for pure enjoyment.  

There are a couple of Blazor changes that were needed, but overall it is fairly straightforward and a ton of fun to implement.

The implementation was to make an API call to get a Dad joke from a Dad Joke service, yes they have those.  Then get a random image to display, then display the image on each request.

The project was inspired by Brad at Traversy Media.  He has a ton of great videos and courses on web development.  He does a good job of explaining the details as well.  You should check him out.


Applications Objective

The application is an entertaining project that just felt good to build.  It will randomly display a dad joke with a random image.

It is that simple.


Blazor Parts

1. Create Project

    I wanted to deploy this project with a custom domain and as cheaply as possible, my choices were:

            A.  Azure App Service

                To have a custom domain with an app service the minimum service trie you can have cost about $10.00 per month.  So an app service is out.

            B. Blob Storage Web Site

                This option allows a custom domain and would be free since our site is so small.  I did not choose this option because I felt the number of steps needed to get the site up and running was too complex, doable but too many.

                1. Create the Storage Account

                2. Set it up for a Web Site

                3. Upload your files

                4. Set Up a CDN (the only way to use the custom domain)

                5. Configure the CName of the domain

                6. Create a Cert for the HTTPS

            C. Static Web Site

                I went with the static website option.  Not because I had just blogged on how to do it, :-) but it is free, for now, easy to set up easily supports a custom domain, and comes with a cert for HTTP.

Now once they start charging for the service, I may change my mind, but for now, I like this option.


Create Project

Since I am going with the  Azure Static Website, I used the StaticWebDev/Blazor-starter template from GitHub.  It streams lines the project creation process and creates everything in my git hub repro.

Clean Up

    1. Remove the counter and weather forecast code since I will not be needing it.

    2. Change the main layout to be just the body attribute

            @inherits LayoutComponentBase

            @Body

    3. Remove the Bootstrap CSS links in the index.html file since we will be providing our own CSS.


Add CSS and images

    I chose to randomly select from 5 different images when the page refreshes.  The trick is to name the images something like img1, img2 ...

Add service for images

    This is a simple service to return a number between 1 and 5

            System.Random rnd = new System.Random();
            int result = rnd.Next(1, 5);
            if (result > 5)
            {
                result = 1;
            }
            return result;

Index Page

This is where most of the work is done.  We have a single div where we display a title, the image, and the joke:

        <div class="container">
            <h3>Blazor Dad Jokes</h3>
            <div><img src="@imagePath" width="200px" height="200px" /></div>
            <div id="joke" class="joke">@joke</div>
            <button id="jokeBtn" class="btn" @onclick="@(()=>GetNewJoke())">Get                 Another Joke</button>
        </div>
There is a click event on the button to get another joke.  This is the same method called on the page initialization.

We have to set the Media Type on the Get API call.

            MediaTypeWithQualityHeaderValue appt = new MediaTypeWithQualityHeaderValue("application/json");
            Http.DefaultRequestHeaders.Accept.Add(appt);

Call the API

            var result = await Http.GetFromJsonAsync<JokeResponse>("https://icanhazdadjoke.com");

Serialize the result into a joke object
            joke = result.Joke;

            internal class JokeResponse
            {
                public string Id { get; set; }
                public string Joke { get; set; }
                public int Status { get; set; }
            }

Set the image Path

            imagePath = $"img/dad{service.GetBackgroundImage()}.jpg";


Deployment

Since I am deploying to Azure static websites, just followed the instructions in my blog post above.

Once it was deployed, I went to my DNS host, NameCheap, and added my CName record.  Less than 5 minutes later my site was running with the custom domain, BlazorDadJokes.  

Once I had that, at NameCheap I added a redirect record to redirect blazordadjokes.com to www.blazordadjokes.com.


Summary

Even though this project was not full of Blazor coding, it was fun.  When it comes down to it, that is all that matters.


Comments

Popular posts from this blog

Yes, Blazor Server can scale!

Blazor new and improved Search Box

Blazor Wizard Step Component