@@ -60,7 +60,10 @@ public string GenerateKey(ControllerContext context, CacheSettings cacheSettings
6060
6161 string areaName = null ;
6262
63- if ( routeData . DataTokens . ContainsKey ( DataTokensKeyArea ) )
63+ if (
64+ routeData . DataTokens . ContainsKey ( DataTokensKeyArea ) &&
65+ routeData . DataTokens [ DataTokensKeyArea ] != null
66+ )
6467 {
6568 areaName = routeData . DataTokens [ DataTokensKeyArea ] . ToString ( ) ;
6669 }
@@ -83,7 +86,6 @@ public string GenerateKey(ControllerContext context, CacheSettings cacheSettings
8386 if ( ! context . IsChildAction )
8487 {
8588 // note that route values take priority over form values and form values take priority over query string values
86-
8789 if ( ( cacheSettings . Options & OutputCacheOptions . IgnoreFormData ) != OutputCacheOptions . IgnoreFormData )
8890 {
8991 foreach ( var formKey in context . HttpContext . Request . Form . AllKeys )
@@ -107,7 +109,7 @@ public string GenerateKey(ControllerContext context, CacheSettings cacheSettings
107109 {
108110 foreach ( var queryStringKey in context . HttpContext . Request . QueryString . AllKeys )
109111 {
110- // queryStringKey is null if url has qs name without value. e.g. test.com?q
112+ // queryStringKey is null if url has as name without value. e.g. test.com?q
111113 if ( queryStringKey == null || routeValues . ContainsKey ( queryStringKey . ToLowerInvariant ( ) ) )
112114 {
113115 continue ;
@@ -133,7 +135,8 @@ public string GenerateKey(ControllerContext context, CacheSettings cacheSettings
133135 else if ( cacheSettings . VaryByParam != "*" )
134136 {
135137 var parameters = cacheSettings . VaryByParam . ToLowerInvariant ( ) . Split ( new [ ] { ';' } , StringSplitOptions . RemoveEmptyEntries ) ;
136- routeValues = new RouteValueDictionary ( routeValues . Where ( x => parameters . Contains ( x . Key ) ) . ToDictionary ( x => x . Key , x => x . Value ) ) ;
138+ routeValues = new RouteValueDictionary ( routeValues . Where ( x => parameters . Contains ( x . Key ) )
139+ . ToDictionary ( x => x . Key , x => x . Value ) ) ;
137140 }
138141 }
139142
@@ -144,6 +147,16 @@ public string GenerateKey(ControllerContext context, CacheSettings cacheSettings
144147 context . HttpContext . ApplicationInstance . GetVaryByCustomString ( HttpContext . Current , cacheSettings . VaryByCustom ) ;
145148 }
146149
150+ if ( ! string . IsNullOrEmpty ( cacheSettings . VaryByHeader ) )
151+ {
152+ var headers = cacheSettings . VaryByHeader . ToLowerInvariant ( ) . Split ( new [ ] { ';' } , StringSplitOptions . RemoveEmptyEntries ) ;
153+ var existingHeaders = context . HttpContext . Request . Headers . AllKeys . Where ( x => headers . Contains ( x . ToLowerInvariant ( ) ) ) ;
154+ foreach ( var header in existingHeaders )
155+ {
156+ routeValues [ header ] = context . HttpContext . Request . Headers [ header ] ;
157+ }
158+ }
159+
147160 var key = _keyBuilder . BuildKey ( controllerName , actionName , routeValues ) ;
148161
149162 return key ;
0 commit comments