Add react tag helper. Clean up code and make it more consistent.

This commit is contained in:
SteveSandersonMS
2015-11-04 12:19:43 -08:00
parent e410affbd8
commit 7c3d22c7b6
14 changed files with 146 additions and 63 deletions

View File

@@ -7,17 +7,10 @@ using Microsoft.AspNet.Http.Extensions;
namespace Microsoft.AspNet.NodeServices.Angular
{
[HtmlTargetElement(Attributes = PrerenderModuleAttributeName)]
public class AngularRunAtServerTagHelper : TagHelper
public class AngularPrerenderTagHelper : TagHelper
{
static StringAsTempFile nodeScript;
static INodeServices fallbackNodeServices; // Used only if no INodeServices was registered with DI
static AngularRunAtServerTagHelper() {
// Consider populating this lazily
var script = EmbeddedResourceReader.Read(typeof (AngularRunAtServerTagHelper), "/Content/Node/angular-rendering.js");
nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit
}
const string PrerenderModuleAttributeName = "asp-ng2-prerender-module";
const string PrerenderExportAttributeName = "asp-ng2-prerender-export";
@@ -30,7 +23,7 @@ namespace Microsoft.AspNet.NodeServices.Angular
private IHttpContextAccessor contextAccessor;
private INodeServices nodeServices;
public AngularRunAtServerTagHelper(IServiceProvider nodeServices, IHttpContextAccessor contextAccessor)
public AngularPrerenderTagHelper(IServiceProvider nodeServices, IHttpContextAccessor contextAccessor)
{
this.contextAccessor = contextAccessor;
this.nodeServices = (INodeServices)nodeServices.GetService(typeof (INodeServices)) ?? fallbackNodeServices;
@@ -44,12 +37,13 @@ namespace Microsoft.AspNet.NodeServices.Angular
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var result = await this.nodeServices.InvokeExport(nodeScript.FileName, "renderComponent", new {
componentModule = this.ModuleName,
componentExport = this.ExportName,
tagName = output.TagName,
baseUrl = UriHelper.GetEncodedUrl(this.contextAccessor.HttpContext.Request)
});
var result = await AngularRenderer.RenderToString(
nodeServices: this.nodeServices,
componentModuleName: this.ModuleName,
componentExportName: this.ExportName,
componentTagName: output.TagName,
requestUrl: UriHelper.GetEncodedUrl(this.contextAccessor.HttpContext.Request)
);
output.SuppressOutput();
output.PostElement.AppendEncoded(result);
}