MVC Razor views not refreshing after making changes to .CSHTML in .NET Core 3

Since .NET Core 3.0 your views in MVC won’t refresh after you change your .CSHTML markup. According to Microsoft this functionality was removed as a consequence of cleaning up the ASP.NET Core shared framework to not depend on Roslyn.

To have your views refresh in .NET Core 3+ you need to install the below package from NuGet

Install a package

and then enable it from your Startup.cs file.

Add services

Creating a DTO class from a JSON string in Visual Studio

If you’ve got back some JSON from a REST API you’ll likely be wanting to use Newtonsoft or System.Text.Json (.NET Core 3.0) to deserialize that JSON into a .NET DTO class to make working with it easier.

To create the DTO you can

1 – Manually examine the JSON and manually create the C# class or
2 – Use an online JSON to C# convertor such as jsonformatter.org or
3 – Use Visual Studios Paste Special -> Paste JSON as Classes option.

Paste JSON as Classes in Visual Studio

This is a little known feature of Visual Studio and is simple to use. For example if I copied the following JSON (taken from json.org/example.html) into my clipboard ..

JSON example

and selected Paste Special -> Paste JSON As Classes from the Edit menu ..

Paste JSON As Classes

Visual Studio would create the following classes ..

Visual Studio generated classes

which you can easily create an instance of by deserializing the same JSON string.

Convert JSON to object

This is a lot easier than manually creating the DTO and while many of the convertors online are great having something like this built in is better. And as you may have noticed above there’s also a Paste XML As Classes option too if you need it.

Extracting Google PageSpeed performance score in .NET

Most webmasters are aware that Google uses performance as a ranking signal for both desktop and mobile searches so its important your site is as fast as it can be. You can check your performance score according to Google in Chrome developer tools or online but since they provide a HTTP API you can get also get performance scores programmatically.

Calling the PageSpeed HTTP API and Serializing the JSON response into a DTO

Calling the API is easy as its a simple GET request as can be seen below…

How to call the PageSpeed API

You just need to make sure you set your key (you can get one from here but a key is not needed if your just testing) and the URL which you want to run the performance check for as query params. You can see above I’ve also set strategy which will default to ‘DESKTOP’ if not set.

After the response comes back (not very fast as the performance check takes a few seconds) I use the new System.Text.Json serializer released in .NET Core 3.0 to convert it into a simple DTO which matches the structure of the JSON. The performance score itself is located in lighthouseResult -> categories -> performance -> score.

As you can see to adhere with VS naming conventions I’ve made the properties start with uppercase and therefore needed to set the PropertyNameCasesInsentive flag above as otherwise the DTO would be empty after serialization.

Simple DTO for storing PageSpeed results

Calling the PageSpeed HTTP API and querying the JSON directly using JsonDocument

The above is nice and neat as we now have a strongly typed object which we could pass to our views etc. In this case however we only care about one property, the performance score itself so having to create a nested class or DTO just for this seems like a lot of effort.

Rather than serializing we can alternatively use JsonDocument and query the JSON string directly by chaining a couple of GetProperty calls together.

Use GetProperty to read JSON properties

Of course to use GetProperty you need to be sure the property will always exist in your JSON as otherwise an exception will be thrown. If your not sure about this use TryGetProperty instead and check if you’ve successfully got an element before moving on…

Use TryGetProperty to read JSON properties

Google PageSpeed Insights API Client Library for .NET

Google also has a PageSpeed API Client Library for .NET however I’ve not really looked into it much yet. Using this library over simple HTTP requests should allow you to have a more strongly typed approach where you don’t have to worry about matching JSON property names etc.

Parsing a nested value from a JSON string in .NET Core 3 without needing a DTO

.NET doesn’t use Newtonsoft by default anymore (since .NET Core 3.0). A lot of the examples online show how to use the new System.Text.Json namespace to extract values from JSON strings by deserializing the strings into full POCO classes (DTOs/ViewModels).

If you only want to extract the value of a particular property and don’t want the overhead of having to create a lot of nested classes, its very simple to extract values directly using the JsonDocument class.

For example if I have the following nested JSON structure (taken from Google PageSpeed API in case your curious) ..

PageSpeed nested JSON Structure

and

I want to extract the 0.82 score value I can just create a new instance of a JsonDocument and then chain a couple of GetProperty calls together.

How to read nested values in System.Text.Json

Of course to use GetProperty you need to be sure the property will always exist in your JSON as otherwise an exception will be thrown. If your not sure about this use TryGetProperty instead and check if you’ve successfully got an element before moving on…

TryGetProperty method

Enabling Azure CDN to read Azure Storage account blobs to resolve ‘The specified resource does not exist’ error

If you’ve got an Azure CDN endpoint hooked into an Azure Storage account you may get an error stating ‘The specified resource does not exist‘ when you try to access a file through the CDN URL endpoint.

If your sure the file exists in your storage account container most likely you’ve not set the access permissions on your storage container to allow anonymous blob access. Blobs that are shared via Azure CDN must support anonymous access. Turning it on is easy.

1 – Navigate to your Azure Storage account and then ‘Containers’ and select the relevant container and click ‘Change access level’

Setting access on a container

2 – Select ‘Blob (anonymous read access for blobs only)’

Azure Storage account container access