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