Azure CDN – Query strings and caching behavior

Azure CDN – Query strings and caching behavior

In this series about Azure CDN, we have tried different things to understand how Azure CDN works. If you want to go through them, below is the list:

In previous post, we have seen how to enable Azure CDN on Azure web app. We will extend further on it in this post. By default, the Azure CDN endpoint would ignore the query string. Meaning, regardless of whether the query string is same or different, same page would be returned by CDN.

In real world, it might not be the case. If the application decides which contents to render on page based on query string, then it might be useful to setup CDN to consider query strings while caching the pages and serving the cached pages.

You would need to have an Azure account with an active subscription.  If you do not have it yet, you can create one for free. And then sign in to the Azure portal.

Prerequisite

Complete the steps from previous post to setup an Azure web app and publish the .NET Core web application.

Modify Application Code

Let’s make simple modifications to the .NET Core web application. In the default page (/home/index action), let’s expect a query string parameter version. Set it to ViewData collection. Then on view render this version after application name. Below code snippet shows the changes done:

// STEP 1
// Code in HomeController.cs, Index action
// New query string parameter – version
public IActionResult Index([FromQuery]string version)
{
ViewData["version"] = version;
return View();
}
// STEP 2
// Code in _Layout.cshtml
// <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">SampleApp @ViewData["version"]</a>
view raw HomeController.cs hosted with ❤ by GitHub

Now, let’s purge everything from Azure CDN endpoint as mentioned in previous post.

Every Querystring Returns Same Page

Now, if you try to access the web page using two different query string parameters:

Both the query strings, would show the text without the value of query string.

Azure CDN and .NET Core Web Application query strings
Azure CDN and .NET Core Web Application query strings

When I tried to both the pages multiple times, randomly showing “first” text after the application name. It means the CDN is not strictly respecting the query string.

Change CDN Settings

Now, let’s navigate to the CDN profile and open the CDN endpoint, which is associated with the Azure web app. Then select the Caching rules under Settings. It would open a new panel.

Under Global caching rules, the field Query string caching behavior has the value Ignore query strings. Now, let’s open the dropdown and select value Cache every unique URL value. Then click on Save button to save the changes.

Azure Portal: Azure CDN Endpoint Caching Rules for Caching Every Unique URL
Azure Portal: Azure CDN Endpoint Caching Rules for Caching Every Unique URL

Once again, let’s purge everything from Azure CDN endpoint as mentioned in previous post. Then give some time to get the settings propagate to all POPs. I started verifications in 20 minutes after purging the CDN endpoint data.

Verification

Now once again, let’s try to access the application using the CDN endpoint. Now use different query strings. Everytime, correct version should be available on the page.

Also, if the same URL is refreshed many times, the page always show correct version .

I hope you found this information useful. Let me know your thoughts.

Leave a Reply