Skip to content

Commit 422d794

Browse files
committed
Checking inside the dictionnary for key presence.
1 parent 60b5722 commit 422d794

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

DevTrends.MvcDonutCaching/DonutOutputCacheAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ 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
147+
// If we are unable to generate a cache key it means we can't do anything
148148
if (string.IsNullOrEmpty(cacheKey))
149149
{
150150
return;

DevTrends.MvcDonutCaching/KeyGenerator.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using DevTrends.MvcDonutCaching.Annotations;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Web;
@@ -9,6 +10,10 @@ namespace DevTrends.MvcDonutCaching
910
{
1011
public class KeyGenerator : IKeyGenerator
1112
{
13+
private const string RouteDataKeyAction = "action";
14+
private const string RouteDataKeyController = "controller";
15+
private const string DataTokensKeyArea = "area";
16+
1217
private readonly IKeyBuilder _keyBuilder;
1318

1419
public KeyGenerator(IKeyBuilder keyBuilder)
@@ -21,6 +26,7 @@ public KeyGenerator(IKeyBuilder keyBuilder)
2126
_keyBuilder = keyBuilder;
2227
}
2328

29+
[CanBeNull]
2430
public string GenerateKey(ControllerContext context, CacheSettings cacheSettings)
2531
{
2632
var routeData = context.RouteData;
@@ -33,14 +39,18 @@ public string GenerateKey(ControllerContext context, CacheSettings cacheSettings
3339
string actionName = null,
3440
controllerName = null;
3541

36-
if (routeData.Values["action"] != null)
42+
if (
43+
routeData.Values.ContainsKey(RouteDataKeyAction) &&
44+
routeData.Values[RouteDataKeyAction] != null)
3745
{
38-
actionName = routeData.Values["action"].ToString();
46+
actionName = routeData.Values[RouteDataKeyAction].ToString();
3947
}
4048

41-
if (routeData.Values["controller"] != null)
49+
if (
50+
routeData.Values.ContainsKey(RouteDataKeyController) &&
51+
routeData.Values[RouteDataKeyController] != null)
4252
{
43-
controllerName = routeData.Values["controller"].ToString();
53+
controllerName = routeData.Values[RouteDataKeyController].ToString();
4454
}
4555

4656
if (string.IsNullOrEmpty(actionName) || string.IsNullOrEmpty(controllerName))
@@ -50,22 +60,22 @@ public string GenerateKey(ControllerContext context, CacheSettings cacheSettings
5060

5161
string areaName = null;
5262

53-
if (routeData.DataTokens.ContainsKey("area"))
63+
if (routeData.DataTokens.ContainsKey(DataTokensKeyArea))
5464
{
55-
areaName = routeData.DataTokens["area"].ToString();
65+
areaName = routeData.DataTokens[DataTokensKeyArea].ToString();
5666
}
5767

5868
// remove controller, action and DictionaryValueProvider which is added by the framework for child actions
5969
var filteredRouteData = routeData.Values.Where(
60-
x => x.Key.ToLowerInvariant() != "controller" &&
61-
x.Key.ToLowerInvariant() != "action" &&
62-
x.Key.ToLowerInvariant() != "area" &&
70+
x => x.Key.ToLowerInvariant() != RouteDataKeyController &&
71+
x.Key.ToLowerInvariant() != RouteDataKeyAction &&
72+
x.Key.ToLowerInvariant() != DataTokensKeyArea &&
6373
!(x.Value is DictionaryValueProvider<object>)
6474
).ToList();
6575

6676
if (!string.IsNullOrWhiteSpace(areaName))
6777
{
68-
filteredRouteData.Add(new KeyValuePair<string, object>("area", areaName));
78+
filteredRouteData.Add(new KeyValuePair<string, object>(DataTokensKeyArea, areaName));
6979
}
7080

7181
var routeValues = new RouteValueDictionary(filteredRouteData.ToDictionary(x => x.Key.ToLowerInvariant(), x => x.Value));

0 commit comments

Comments
 (0)