forked from pauldotknopf/JavaScriptViewEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeRenderEngine.cs
More file actions
82 lines (76 loc) · 2.97 KB
/
NodeRenderEngine.cs
File metadata and controls
82 lines (76 loc) · 2.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
using System.Threading.Tasks;
using Microsoft.AspNetCore.NodeServices;
using JavaScriptViewEngine.Utils;
#if DOTNETCORE
using Microsoft.AspNetCore.Routing;
#else
using System.Web.Routing;
#endif
namespace JavaScriptViewEngine
{
/// <summary>
/// The default implementing of <see cref="IRenderEngine"/> that uses node
/// </summary>
/// <seealso cref="IRenderEngine" />
public class NodeRenderEngine : IRenderEngine
{
private readonly NodeRenderEngineOptions _options;
private INodeServices _nodeServices;
/// <summary>
/// Initializes a new instance of the <see cref="NodeRenderEngine" /> class.
/// </summary>
/// <param name="options">The options.</param>
public NodeRenderEngine(NodeRenderEngineOptions options)
{
_options = options;
_nodeServices = Configuration.CreateNodeServices(new NodeServicesOptions
{
HostingModel = NodeHostingModel.Http,
ProjectPath = options.ProjectDirectory
});
}
/// <summary>
/// Perform a render
/// </summary>
/// <param name="path">The path.</param>
/// <param name="model">The model.</param>
/// <param name="viewBag">The view bag.</param>
/// <param name="routeValues">The route values.</param>
/// <param name="area">The area.</param>
/// <param name="viewType">Type of the view.</param>
/// <returns></returns>
public Task<RenderResult> RenderAsync(string path, object model, dynamic viewBag, RouteValueDictionary routeValues, string area, ViewType viewType)
{
if (_options.GetArea != null)
area = _options.GetArea(area);
return _nodeServices.InvokeExport<RenderResult>(area,
viewType == ViewType.Full ? "renderView" : "renderPartialView",
path,
model,
viewBag,
routeValues);
}
/// <summary>
/// Perform a render
/// </summary>
/// <param name="path">The path.</param>
/// <param name="model">The model.</param>
/// <param name="viewBag">The view bag.</param>
/// <param name="routeValues">The route values.</param>
/// <param name="area">The area.</param>
/// <param name="viewType">Type of the view.</param>
/// <returns></returns>
public RenderResult Render(string path, object model, dynamic viewBag, RouteValueDictionary routeValues, string area, ViewType viewType)
{
return AsyncHelpers.RunSync<RenderResult>(() => RenderAsync(path, model, viewBag, routeValues, area, viewType));
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
_nodeServices?.Dispose();
_nodeServices = null;
}
}
}