forked from pauldotknopf/JavaScriptViewEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsViewEngine.cs
More file actions
210 lines (178 loc) · 7.67 KB
/
JsViewEngine.cs
File metadata and controls
210 lines (178 loc) · 7.67 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.IO;
using System.Threading.Tasks;
#if DOTNETCORE
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewEngines;
#else
using System.Web.Mvc;
#endif
#if DI
using Microsoft.Extensions.Options;
#endif
namespace JavaScriptViewEngine
{
/// <summary>
/// The aspnet view engine that will pass the model to a render engine to render the markup.
/// </summary>
public class JsViewEngine : IJsViewEngine
{
private readonly JsViewEngineOptions _options;
/// <summary>
/// Initializes a new instance of the <see cref="JsViewEngine" /> class.
/// </summary>
/// <param name="options">The options.</param>
public JsViewEngine(IOptions<JsViewEngineOptions> options)
{
_options = options.Value;
}
#if DOTNETCORE
public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage)
{
if (!string.IsNullOrEmpty(_options.ViewNamePrefix))
{
if (!viewName.StartsWith(_options.ViewNamePrefix))
return ViewEngineResult.NotFound(viewName, new string[] { viewName });
}
return ViewEngineResult.Found(viewName, new JsView
{
Path = !string.IsNullOrEmpty(_options.ViewNamePrefix) ? viewName.Substring(_options.ViewNamePrefix.Length) : viewName,
ViewType = ViewType.Full
});
}
public ViewEngineResult GetView(string executingFilePath, string viewPath, bool isMainPage)
{
if (!string.IsNullOrEmpty(_options.ViewNamePrefix))
{
if (!viewPath.StartsWith(_options.ViewNamePrefix))
return ViewEngineResult.NotFound(viewPath, new string[] { viewPath });
}
return ViewEngineResult.Found(viewPath, new JsView
{
Path = !string.IsNullOrEmpty(_options.ViewNamePrefix) ? viewPath.Substring(_options.ViewNamePrefix.Length) : viewPath,
ViewType = ViewType.Full
});
}
#else
public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
if (!string.IsNullOrEmpty(_options.ViewNamePrefix))
{
if (!partialViewName.StartsWith(_options.ViewNamePrefix))
return new ViewEngineResult(new string[] { partialViewName });
}
return new ViewEngineResult(new JsView
{
Path = !string.IsNullOrEmpty(_options.ViewNamePrefix) ? partialViewName.Substring(_options.ViewNamePrefix.Length) : partialViewName,
ViewType = ViewType.Partial
}, this);
}
public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
if (!string.IsNullOrEmpty(_options.ViewNamePrefix))
{
if (!viewName.StartsWith(_options.ViewNamePrefix))
return new ViewEngineResult(new string[] { viewName });
}
return new ViewEngineResult(new JsView
{
Path = !string.IsNullOrEmpty(_options.ViewNamePrefix) ? viewName.Substring(_options.ViewNamePrefix.Length) : viewName,
ViewType = ViewType.Full
}, this);
}
public void ReleaseView(ControllerContext controllerContext, IView view)
{
}
#endif
/// <summary>
/// The view that invokes a javascript engine with the model, and writes the output to the response.
/// </summary>
/// <seealso cref="System.Web.Mvc.IView" />
/// <seealso cref="IView" />
public class JsView : IView
{
/// <summary>
/// The path that get's sent to the javascript method.
/// </summary>
public string Path { get; set; }
/// <summary>
/// The type of view that is being rendered.
/// </summary>
public ViewType ViewType { get; set; }
#if DOTNETCORE
public async Task RenderAsync(ViewContext context)
{
var renderEngine = context.HttpContext.Request.HttpContext.Items["RenderEngine"] as IRenderEngine;
if (renderEngine == null) throw new Exception("Couldn't get IRenderEngine from the context request items.");
var path = Path;
if (string.Equals(path, "{auto}", StringComparison.OrdinalIgnoreCase))
{
path = context.HttpContext.Request.Path;
if (context.HttpContext.Request.QueryString.HasValue)
{
path += context.HttpContext.Request.QueryString.Value;
}
}
string areaObject;
context.ActionDescriptor.RouteValues.TryGetValue("area", out areaObject);
if (areaObject == null)
{
areaObject = "default";
}
var result = await renderEngine.RenderAsync(path, context.ViewData.Model, context.ViewBag, context.RouteData.Values, areaObject, ViewType);
if (ViewType == ViewType.Full)
{
if (!string.IsNullOrEmpty(result.Redirect))
{
context.HttpContext.Response.Redirect(result.Redirect);
return;
}
context.HttpContext.Response.StatusCode = result.Status;
await context.Writer.WriteAsync(result.Html);
}
else
{
await context.Writer.WriteAsync(result.Html);
}
}
#else
public void Render(ViewContext viewContext, TextWriter writer)
{
var renderEngine = viewContext.HttpContext.Items["RenderEngine"] as IRenderEngine;
if (renderEngine == null) throw new Exception("Couldn't get IRenderEngine from the context request items.");
var path = Path;
if (string.Equals(path, "{auto}", StringComparison.OrdinalIgnoreCase))
{
path = viewContext.HttpContext.Request.Path;
if (viewContext.HttpContext.Request.QueryString != null && viewContext.HttpContext.Request.QueryString.Count > 0)
{
path += "?" + viewContext.HttpContext.Request.QueryString.ToString();
}
}
object areaObject;
viewContext.RouteData.Values.TryGetValue("area", out areaObject);
if (areaObject == null)
{
areaObject = "default";
}
var result = renderEngine.Render(path, viewContext.ViewData.Model, viewContext.ViewBag, viewContext.RouteData.Values, areaObject.ToString(), ViewType);
if (ViewType == ViewType.Full)
{
if (!string.IsNullOrEmpty(result.Redirect))
{
viewContext.HttpContext.Response.Redirect(result.Redirect);
return;
}
viewContext.HttpContext.Response.StatusCode = result.Status;
writer.Write(result.Html);
}
else
{
writer.Write(result.Html);
}
}
#endif
}
}
}