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
56 lines (43 loc) · 1.84 KB
/
JsViewEngine.cs
File metadata and controls
56 lines (43 loc) · 1.84 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
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.ViewEngines;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
namespace JavaScriptViewEngine
{
public class JsViewEngine : IViewEngine
{
private IJsEngineInvoker _jsEngineInvoker;
public JsViewEngine(IJsEngineInvoker jsEngineInvoker = null)
{
if(jsEngineInvoker == null)
jsEngineInvoker = new JsEngineInvoker();
_jsEngineInvoker = jsEngineInvoker;
}
public ViewEngineResult FindPartialView(ActionContext context, string partialViewName)
{
return ViewEngineResult.Found(partialViewName, new ReactView(_jsEngineInvoker) { ViewType = ViewType.Partial, Path = partialViewName });
}
public ViewEngineResult FindView(ActionContext context, string viewName)
{
return ViewEngineResult.Found(viewName, new ReactView(_jsEngineInvoker) { ViewType = ViewType.Full, Path = viewName });
}
class ReactView : IView
{
private IJsEngineInvoker _jsEngineInvoker;
public ReactView(IJsEngineInvoker jsEngineInvoker)
{
_jsEngineInvoker = jsEngineInvoker;
}
public string Path { get; set; }
public ViewType ViewType { get; set; }
public async Task RenderAsync(ViewContext context)
{
var jsEngine = context.HttpContext.Request.HttpContext.Items["JsEngine"] as IJsEngine;
if (jsEngine == null) throw new Exception("Couldn't get IJsEngine from the context request items.");
var result = await _jsEngineInvoker.InvokeEngine(jsEngine, ViewType, Path, context);
await context.Writer.WriteAsync(result);
}
}
}
}