diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/Internal/FileVersionProvider.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/Internal/FileVersionProvider.cs
index 6d5a4a989d..dd8678f344 100644
--- a/src/Microsoft.AspNet.Mvc.TagHelpers/Internal/FileVersionProvider.cs
+++ b/src/Microsoft.AspNet.Mvc.TagHelpers/Internal/FileVersionProvider.cs
@@ -16,6 +16,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
public class FileVersionProvider
{
private const string VersionKey = "v";
+ private static readonly char[] QueryStringAndFragmentTokens = new [] { '?', '#' };
private readonly IFileProvider _fileProvider;
private readonly IMemoryCache _cache;
private readonly PathString _requestPathBase;
@@ -24,8 +25,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
/// Creates a new instance of .
///
/// The file provider to get and watch files.
- /// Name of the application.
/// where versioned urls of files are cached.
+ /// The base path for the current HTTP request.
public FileVersionProvider(
IFileProvider fileProvider,
IMemoryCache cache,
@@ -52,7 +53,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
/// The path of the file to which version should be added.
/// Path containing the version query string.
///
- /// The version query string is appended as with the key "v".
+ /// The version query string is appended with the key "v".
///
public string AddFileVersionToPath(string path)
{
@@ -63,7 +64,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
var resolvedPath = path;
- var queryStringOrFragmentStartIndex = path.IndexOfAny(new char[] { '?', '#' });
+ var queryStringOrFragmentStartIndex = path.IndexOfAny(QueryStringAndFragmentTokens);
if (queryStringOrFragmentStartIndex != -1)
{
resolvedPath = path.Substring(0, queryStringOrFragmentStartIndex);
@@ -76,35 +77,39 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
return path;
}
- var fileInfo = _fileProvider.GetFileInfo(resolvedPath);
- if (!fileInfo.Exists)
- {
- if (_requestPathBase.HasValue &&
- resolvedPath.StartsWith(_requestPathBase.Value, StringComparison.OrdinalIgnoreCase))
- {
- resolvedPath = resolvedPath.Substring(_requestPathBase.Value.Length);
- fileInfo = _fileProvider.GetFileInfo(resolvedPath);
- }
-
- if (!fileInfo.Exists)
- {
- // if the file is not in the current server.
- return path;
- }
- }
-
string value;
if (!_cache.TryGetValue(path, out value))
{
- value = QueryHelpers.AddQueryString(path, VersionKey, GetHashForFile(fileInfo));
- var cacheEntryOptions = new MemoryCacheEntryOptions().AddExpirationToken(_fileProvider.Watch(resolvedPath));
- _cache.Set(path, value, cacheEntryOptions);
+ var cacheEntryOptions = new MemoryCacheEntryOptions();
+ cacheEntryOptions.AddExpirationToken(_fileProvider.Watch(resolvedPath));
+ var fileInfo = _fileProvider.GetFileInfo(resolvedPath);
+
+ if (!fileInfo.Exists &&
+ _requestPathBase.HasValue &&
+ resolvedPath.StartsWith(_requestPathBase.Value, StringComparison.OrdinalIgnoreCase))
+ {
+ var requestPathBaseRelativePath = resolvedPath.Substring(_requestPathBase.Value.Length);
+ cacheEntryOptions.AddExpirationToken(_fileProvider.Watch(requestPathBaseRelativePath));
+ fileInfo = _fileProvider.GetFileInfo(requestPathBaseRelativePath);
+ }
+
+ if (fileInfo.Exists)
+ {
+ value = QueryHelpers.AddQueryString(path, VersionKey, GetHashForFile(fileInfo));
+ }
+ else
+ {
+ // if the file is not in the current server.
+ value = path;
+ }
+
+ value = _cache.Set(path, value, cacheEntryOptions);
}
return value;
}
- private string GetHashForFile(IFileInfo fileInfo)
+ private static string GetHashForFile(IFileInfo fileInfo)
{
using (var sha256 = SHA256.Create())
{
diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ImageTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ImageTagHelperTest.cs
index b022fffe45..eaec7657f9 100644
--- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ImageTagHelperTest.cs
+++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ImageTagHelperTest.cs
@@ -325,21 +325,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
return hostingEnvironment.Object;
}
- private static IMemoryCache MakeCache()
- {
- object result = null;
- var cache = new Mock();
- cache.CallBase = true;
- cache.Setup(c => c.TryGetValue(It.IsAny(), out result))
- .Returns(result != null);
- cache.Setup(
- c => c.Set(
- /*key*/ It.IsAny(),
- /*value*/ It.IsAny