ASP.NET Web API 2 – Sitecore HTML Cache Action Filter with ETag

You do not need to write this code every time ...

You do not need to write this code every time …

Nowadays, almost every Sitecore solution needs a custom Web API endpoint implementation. One of the common web API uses cases when a front-end component needs to be dynamically rendered at the client-side (eg a browser page). However, there could be some challenging factors of the back-end implementation:

  1. The Web API response data needs to be presented in JSON format.
  2. The Web API response data is calculated based on multiple Sitecore items.
    – The data evaluation is expensive in the performance perspective so it has to be cached.
    – The data cache needs to be invalidated when the corresponding Sitecore items are changed.
  3. The data to be transferred over the network only the data has been changed, otherwise, the client-side cached version of it will be used.

The Solution

To serve the data in JSON format via Web API endpoint, it is possible to use ASP.NET Web API 2. Therefore, no significant challenges here.

To cache the data Sitecore HTML Cache can be used. Sitecore HTML Cache invalidates automatically by Sitecore (normally, on Publish). In this case, the Controller Action would need to produce the data and let Sitecore HTML Cache store it. However, it would be good to remove the cache storage responsibility from the Controller Action. In addition, it would be necessary to avoid Controller Action execution if the data already exists in the cache. Luckily,  ASP.NET Web API 2 Action Filter attributes exist that can help to do this. The Action Filter attribute has two handlers:

  • OnActionExecuting – allows accessing the current request context, including the request parameter object (s) and terminate the request with a custom response without going ahead with the Controller Action execution.
  • OnActionExecuted – allows accessing the response result in the cache.

To address the redundant data transfer between the server and the client, HTTP ETag mechanism can be used which allows producing 304 Not Modified HTTP response. Again, A SP.NET Web API 2 can help to solve this. 

HTTP ETag mechanism, including Sitecore HTML Cache, can be applied in a generic way to any controller using Action Filter attributes.

The Code

(mehr …)

How to Create a Rule in Sitecore to automatically fill an empty field with existing content

Using the Sitecore Rule engine for solving a use case for automatically filling a field in Sitecore, when saving a page item: an empty Page Description field should inherit the content of an existing Introduction field. Weiterlesen

Analysis of an issue with Sitecore AccessResultCache running full and being cleared continuously despite large cache size

Disaster girl meme adapted for explaining impact of expensive Sitecore item query on AccessResultCache

We recently had a situation, where we quickly had to grab a virtual whip in order to tame the Sitecore AccessResultCache from going crazy – in terms of reaching it’s allocated size within seconds and thus being constantly cleared by Sitecore.Caching.Generics.Cache

Store Item „Published date“ and „Published by“ information on each Sitecore item

Sitecore Publish Statistics for Published by and Published date - Teaser

Did you ever miss that Sitecore doesn’t keep track of who published which item(s) at what date? Well, be not sad no longer! In this blog post you’ll find a neat solution to store the „Published date“ and „Published by [user]“ information on all Sitecore items – so it’s finally possible to blame the right person who published too many items once again 😉 Weiterlesen

Delivering multilingual content using Sitecore: the right content in the right language for your visitors

Sitecore Delivering Right Content in Right Language - Teaser

This article describes a working approach and use case for evaluating your website visitors country and language preferences, in order to deliver them the right content in the right language using the Sitecore Experience Platform. Weiterlesen

How to create a simple SXA component

SXA contains the Toolbox feature, which provides Authors the possibility to drag and drop several components in order to add it to pages in the Experience Editor. You can also create a custom component and use it in the Toolbox like … Weiterlesen

Titles, Headings, Descriptions and Structured Data with Sitecore 9 and Razor (SEO)

Product Item

The purpose of a Search Engine Optimization (SEO) campaign is to get more people on your website. But as you as developer probably know, SEO is more of a process than a product. We can’t make a website…”seo”… But we … Weiterlesen

Sitecore field types – a visual cheat sheet

Sitecore Field Types Screenshots - Teaser

There is no actual *visual* documentation of the currently available field types in Sitecore. In this post we are filling that gap and provide you with actual screenshots in addition to the existing documentations. Weiterlesen

How to automatically enable/disable a Sitecore CMS user account at a specific date-time

Sitecore auto enable disable CMS user account - Teaser

When you need a Sitecore CMS user account to be only valid during a specific time period – or automatically disabled at a certain point in time, we present you a neat solution to achieve this here. Weiterlesen