Skip to content

Commit 60b5722

Browse files
committed
For some reason, sometimes routes values can be null.
Closes : #26, Refs: #19
1 parent 5e2677e commit 60b5722

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

DevTrends.MvcDonutCaching/DonutOutputCacheAttribute.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
144144

145145
var cacheKey = KeyGenerator.GenerateKey(filterContext, CacheSettings);
146146

147+
// If we are unable to generate a cache key it mean we can't do anything
148+
if (string.IsNullOrEmpty(cacheKey))
149+
{
150+
return;
151+
}
152+
147153
// Are we actually storing data on the server side ?
148154
if (CacheSettings.IsServerCachingEnabled)
149155
{
@@ -302,6 +308,11 @@ private void ExecuteCallback(ControllerContext context, bool hasErrors)
302308
{
303309
var cacheKey = KeyGenerator.GenerateKey(context, CacheSettings);
304310

311+
if (string.IsNullOrEmpty(cacheKey))
312+
{
313+
return;
314+
}
315+
305316
var callback = context.HttpContext.Items[cacheKey] as Action<bool>;
306317

307318
if (callback != null)

DevTrends.MvcDonutCaching/KeyGenerator.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,40 @@ public KeyGenerator(IKeyBuilder keyBuilder)
2323

2424
public string GenerateKey(ControllerContext context, CacheSettings cacheSettings)
2525
{
26-
var actionName = context.RouteData.Values["action"].ToString();
27-
var controllerName = context.RouteData.Values["controller"].ToString();
28-
string areaName = null;
26+
var routeData = context.RouteData;
2927

30-
if (context.RouteData.DataTokens.ContainsKey("area"))
28+
if (routeData == null)
3129
{
32-
areaName = context.RouteData.DataTokens["area"].ToString();
30+
return null;
31+
}
32+
33+
string actionName = null,
34+
controllerName = null;
35+
36+
if (routeData.Values["action"] != null)
37+
{
38+
actionName = routeData.Values["action"].ToString();
39+
}
40+
41+
if (routeData.Values["controller"] != null)
42+
{
43+
controllerName = routeData.Values["controller"].ToString();
44+
}
45+
46+
if (string.IsNullOrEmpty(actionName) || string.IsNullOrEmpty(controllerName))
47+
{
48+
return null;
49+
}
50+
51+
string areaName = null;
52+
53+
if (routeData.DataTokens.ContainsKey("area"))
54+
{
55+
areaName = routeData.DataTokens["area"].ToString();
3356
}
3457

3558
// remove controller, action and DictionaryValueProvider which is added by the framework for child actions
36-
var filteredRouteData = context.RouteData.Values.Where(
59+
var filteredRouteData = routeData.Values.Where(
3760
x => x.Key.ToLowerInvariant() != "controller" &&
3861
x.Key.ToLowerInvariant() != "action" &&
3962
x.Key.ToLowerInvariant() != "area" &&

0 commit comments

Comments
 (0)