forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebRazorHostFactory.cs
More file actions
178 lines (152 loc) · 6.97 KB
/
WebRazorHostFactory.cs
File metadata and controls
178 lines (152 loc) · 6.97 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Compilation;
using System.Web.Configuration;
using System.Web.Hosting;
using System.Web.WebPages.Razor.Configuration;
using System.Web.WebPages.Razor.Resources;
using Microsoft.Internal.Web.Utils;
namespace System.Web.WebPages.Razor
{
public class WebRazorHostFactory
{
private static ConcurrentDictionary<string, Func<WebRazorHostFactory>> _factories =
new ConcurrentDictionary<string, Func<WebRazorHostFactory>>(StringComparer.OrdinalIgnoreCase);
internal static Func<string, Type> TypeFactory = DefaultTypeFactory;
public static WebPageRazorHost CreateDefaultHost(string virtualPath)
{
return CreateDefaultHost(virtualPath, null);
}
public static WebPageRazorHost CreateDefaultHost(string virtualPath, string physicalPath)
{
return CreateHostFromConfigCore(null, virtualPath, physicalPath);
}
public static WebPageRazorHost CreateHostFromConfig(string virtualPath)
{
return CreateHostFromConfig(virtualPath, null);
}
public static WebPageRazorHost CreateHostFromConfig(string virtualPath, string physicalPath)
{
if (String.IsNullOrEmpty(virtualPath))
{
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, CommonResources.Argument_Cannot_Be_Null_Or_Empty, "virtualPath"), "virtualPath");
}
return CreateHostFromConfigCore(GetRazorSection(virtualPath), virtualPath, physicalPath);
}
public static WebPageRazorHost CreateHostFromConfig(RazorWebSectionGroup config, string virtualPath)
{
return CreateHostFromConfig(config, virtualPath, null);
}
public static WebPageRazorHost CreateHostFromConfig(RazorWebSectionGroup config, string virtualPath, string physicalPath)
{
if (config == null)
{
throw new ArgumentNullException("config");
}
if (String.IsNullOrEmpty(virtualPath))
{
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, CommonResources.Argument_Cannot_Be_Null_Or_Empty, "virtualPath"), "virtualPath");
}
return CreateHostFromConfigCore(config, virtualPath, physicalPath);
}
internal static WebPageRazorHost CreateHostFromConfigCore(RazorWebSectionGroup config, string virtualPath, string physicalPath)
{
// Use the virtual path to select a host environment for the generated code
// Do this check here because the App_Code host can't be overridden.
// Make the path app relative
virtualPath = EnsureAppRelative(virtualPath);
WebPageRazorHost host;
if (virtualPath.StartsWith("~/App_Code", StringComparison.OrdinalIgnoreCase))
{
// Under App_Code => It's a Web Code file
host = new WebCodeRazorHost(virtualPath, physicalPath);
}
else
{
WebRazorHostFactory factory = null;
if (config != null && config.Host != null && !String.IsNullOrEmpty(config.Host.FactoryType))
{
Func<WebRazorHostFactory> factoryCreator = _factories.GetOrAdd(config.Host.FactoryType, CreateFactory);
Debug.Assert(factoryCreator != null); // CreateFactory should throw if there's an error creating the factory
factory = factoryCreator();
}
host = (factory ?? new WebRazorHostFactory()).CreateHost(virtualPath, physicalPath);
if (config != null && config.Pages != null)
{
ApplyConfigurationToHost(config.Pages, host);
}
}
return host;
}
private static Func<WebRazorHostFactory> CreateFactory(string typeName)
{
Type factoryType = TypeFactory(typeName);
if (factoryType == null)
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
RazorWebResources.Could_Not_Locate_FactoryType,
typeName));
}
return Expression.Lambda<Func<WebRazorHostFactory>>(Expression.New(factoryType))
.Compile();
}
public static void ApplyConfigurationToHost(RazorPagesSection config, WebPageRazorHost host)
{
host.DefaultPageBaseClass = config.PageBaseType;
// Add imports
foreach (string import in config.Namespaces.OfType<NamespaceInfo>().Select(ns => ns.Namespace))
{
host.NamespaceImports.Add(import);
}
}
public virtual WebPageRazorHost CreateHost(string virtualPath, string physicalPath)
{
return new WebPageRazorHost(virtualPath, physicalPath);
}
internal static RazorWebSectionGroup GetRazorSection(string virtualPath)
{
// Get the individual sections (we can only use GetSection in medium trust) and then reconstruct the section group
return new RazorWebSectionGroup()
{
Host = (HostSection)WebConfigurationManager.GetSection(HostSection.SectionName, virtualPath),
Pages = (RazorPagesSection)WebConfigurationManager.GetSection(RazorPagesSection.SectionName, virtualPath)
};
}
#if CODE_COVERAGE
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
// JUSTIFICATION: VirtualPathUtility.ToAppRelative is only available in ASP.Net environment
#endif
private static string EnsureAppRelative(string virtualPath)
{
if (HostingEnvironment.IsHosted)
{
virtualPath = VirtualPathUtility.ToAppRelative(virtualPath);
}
else
{
if (virtualPath.StartsWith("/", StringComparison.Ordinal))
{
virtualPath = "~" + virtualPath;
}
else if (!virtualPath.StartsWith("~/", StringComparison.Ordinal))
{
virtualPath = "~/" + virtualPath;
}
}
return virtualPath;
}
#if CODE_COVERAGE
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
// JUSTIFICATION: BuildManager.GetType is only available in ASP.Net environment
#endif
private static Type DefaultTypeFactory(string typeName)
{
return BuildManager.GetType(typeName, false, false);
}
}
}