-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathPrecompilationVirtualPathFactory.cs
More file actions
62 lines (57 loc) · 2.3 KB
/
PrecompilationVirtualPathFactory.cs
File metadata and controls
62 lines (57 loc) · 2.3 KB
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
using System;
using System.Web.WebPages;
namespace StackExchange.Precompilation
{
/// <summary>
/// <see cref="WebPageExecutingBase.VirtualPathFactory"/> is used for resolving virtual paths once a view has
/// been resolved. Setting it to an <see cref="IVirtualPathFactory"/> assumes that all underlying virtual paths
/// are resolvable (layouts partials etc) are resolvable using the given instance. This can lead to some side
/// effect when different view engines are combined, and the matching views are intermixed.
/// </summary>
/// <remarks>
/// This is our version of the <see cref="VirtualPathFactoryManager"/>, which is meant for extending the pipeline,
/// mentioned above, but is not extendable in the sense that it always falls back to <see cref="System.Web.Compilation.BuildManager"/>,
/// which calls the old csc.exe in the framework dir, not the shiny one from our nuget package,
/// and can thus cause unexpected behavior at runtime.
/// </remarks>
internal class PrecompilationVirtualPathFactory : IVirtualPathFactory
{
private readonly PrecompiledViewEngine _precompiled;
private readonly RoslynRazorViewEngine _runtime;
public PrecompilationVirtualPathFactory(PrecompiledViewEngine precompiled = null, RoslynRazorViewEngine runtime = null)
{
_precompiled = precompiled;
_runtime = runtime;
}
public object CreateInstance(string virtualPath)
{
if (_precompiled?.TryLookupCompiledType(virtualPath) is Type precompiledType)
{
return Activator.CreateInstance(precompiledType);
}
else if (_runtime?.GetTypeFromVirtualPath(virtualPath) is Type runtimeType)
{
return Activator.CreateInstance(runtimeType);
}
else
{
return null;
}
}
public bool Exists(string virtualPath)
{
if (_precompiled?.TryLookupCompiledType(virtualPath) != null)
{
return true;
}
else if (_runtime?.FileExists(virtualPath) == true)
{
return true;
}
else
{
return false;
}
}
}
}