|
| 1 | +using System.Threading.Tasks; |
| 2 | +using Microsoft.AspNet.Razor.Runtime.TagHelpers; |
| 3 | +using Microsoft.AspNet.Http; |
| 4 | +using Microsoft.AspNet.NodeServices; |
| 5 | +using Microsoft.AspNet.Http.Extensions; |
| 6 | + |
| 7 | +namespace Microsoft.AspNet.NodeServices.Angular |
| 8 | +{ |
| 9 | + [HtmlTargetElement(Attributes = PrerenderModuleAttributeName)] |
| 10 | + public class AngularRunAtServerTagHelper : TagHelper |
| 11 | + { |
| 12 | + static StringAsTempFile nodeScript; |
| 13 | + |
| 14 | + static AngularRunAtServerTagHelper() { |
| 15 | + // Consider populating this lazily |
| 16 | + var script = EmbeddedResourceReader.Read(typeof (AngularRunAtServerTagHelper), "/Content/Node/angular-rendering.js"); |
| 17 | + nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit |
| 18 | + } |
| 19 | + |
| 20 | + const string PrerenderModuleAttributeName = "aspnet-ng2-prerender-module"; |
| 21 | + const string PrerenderExportAttributeName = "aspnet-ng2-prerender-export"; |
| 22 | + |
| 23 | + private static NodeInstance nodeInstance = new NodeInstance(); |
| 24 | + |
| 25 | + [HtmlAttributeName(PrerenderModuleAttributeName)] |
| 26 | + public string ModuleName { get; set; } |
| 27 | + |
| 28 | + [HtmlAttributeName(PrerenderExportAttributeName)] |
| 29 | + public string ExportName { get; set; } |
| 30 | + |
| 31 | + private IHttpContextAccessor contextAccessor; |
| 32 | + |
| 33 | + public AngularRunAtServerTagHelper(IHttpContextAccessor contextAccessor) |
| 34 | + { |
| 35 | + this.contextAccessor = contextAccessor; |
| 36 | + } |
| 37 | + |
| 38 | + public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) |
| 39 | + { |
| 40 | + var result = await nodeInstance.InvokeExport(nodeScript.FileName, "renderComponent", new { |
| 41 | + componentModule = this.ModuleName, |
| 42 | + componentExport = this.ExportName, |
| 43 | + tagName = output.TagName, |
| 44 | + baseUrl = UriHelper.GetEncodedUrl(this.contextAccessor.HttpContext.Request) |
| 45 | + }); |
| 46 | + output.SuppressOutput(); |
| 47 | + output.PostElement.AppendEncoded(result); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments