private static string GetRouteLevelMatch(string pathValue, string[] supportedExtensions, Func<string, bool> virtualPathExists, HttpContextBase context, DisplayModeProvider displayModeProvider)
{
for (int i = 0; i < supportedExtensions.Length; i++)
{
string supportedExtension = supportedExtensions[i];
// For performance, avoid multiple calls to String.Concat
string virtualPath;
// Only add the extension if it's not already there
if (!PathHelpers.EndsWithExtension(pathValue, supportedExtension))
{
virtualPath = "~/" + pathValue + "." + supportedExtension;
}
else
{
virtualPath = "~/" + pathValue;
}
DisplayInfo virtualPathDisplayInfo = displayModeProvider.GetDisplayInfoForVirtualPath(virtualPath, context, virtualPathExists, currentDisplayMode: null);
if (virtualPathDisplayInfo != null)
{
// If there's an exact match on disk, return it unless it starts with an underscore
if (Path.GetFileName(virtualPathDisplayInfo.FilePath).StartsWith("_", StringComparison.OrdinalIgnoreCase))
{
throw new HttpException(404, WebPageResources.WebPageRoute_UnderscoreBlocked);
}
string resolvedVirtualPath = virtualPathDisplayInfo.FilePath;
// Matches are not expected to be virtual paths so remove the ~/ from the match
if (resolvedVirtualPath.StartsWith("~/", StringComparison.OrdinalIgnoreCase))
{
resolvedVirtualPath = resolvedVirtualPath.Remove(0, 2);
}
DisplayModeProvider.SetDisplayMode(context, virtualPathDisplayInfo.DisplayMode);
return resolvedVirtualPath;
}
}
return null;
}