1 // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
2 
3 using System.IO;
4 
5 namespace System.Web.WebPages
6 {
7     /// <summary>
8     /// The <see cref="DefaultDisplayMode"/> can take any suffix and determine if there is a corresponding
9     /// file that exists given a path and request by transforming the path to contain the suffix.
10     /// Add a new DefaultDisplayMode to the Modes collection to handle a new suffix or inherit from
11     /// DefaultDisplayMode to provide custom logic to transform paths with a suffix.
12     /// </summary>
13     public class DefaultDisplayMode : IDisplayMode
14     {
15         private readonly string _suffix;
16 
DefaultDisplayMode()17         public DefaultDisplayMode()
18             : this(DisplayModeProvider.DefaultDisplayModeId)
19         {
20         }
21 
DefaultDisplayMode(string suffix)22         public DefaultDisplayMode(string suffix)
23         {
24             _suffix = suffix ?? String.Empty;
25         }
26 
27         /// <summary>
28         /// When set, the <see cref="DefaultDisplayMode"/> will only be available to return Display Info for a request
29         /// if the ContextCondition evaluates to true.
30         /// </summary>
31         public Func<HttpContextBase, bool> ContextCondition { get; set; }
32 
33         public virtual string DisplayModeId
34         {
35             get { return _suffix; }
36         }
37 
CanHandleContext(HttpContextBase httpContext)38         public bool CanHandleContext(HttpContextBase httpContext)
39         {
40             return ContextCondition == null || ContextCondition(httpContext);
41         }
42 
43         /// <summary>
44         /// Returns DisplayInfo with the transformed path if it exists.
45         /// </summary>
GetDisplayInfo(HttpContextBase httpContext, string virtualPath, Func<string, bool> virtualPathExists)46         public virtual DisplayInfo GetDisplayInfo(HttpContextBase httpContext, string virtualPath, Func<string, bool> virtualPathExists)
47         {
48             string transformedFilename = TransformPath(virtualPath, _suffix);
49             if (transformedFilename != null && virtualPathExists(transformedFilename))
50             {
51                 return new DisplayInfo(transformedFilename, this);
52             }
53 
54             return null;
55         }
56 
57         /// <summary>
58         /// Transforms paths according to the following rules:
59         /// \some\path.blah\file.txt.zip -> \some\path.blah\file.txt.suffix.zip
60         /// \some\path.blah\file -> \some\path.blah\file.suffix
61         /// </summary>
TransformPath(string virtualPath, string suffix)62         protected virtual string TransformPath(string virtualPath, string suffix)
63         {
64             if (String.IsNullOrEmpty(suffix))
65             {
66                 return virtualPath;
67             }
68 
69             string extension = Path.GetExtension(virtualPath);
70             return Path.ChangeExtension(virtualPath, suffix + extension);
71         }
72     }
73 }
74