React.NET icon indicating copy to clipboard operation
React.NET copied to clipboard

Server-side rendering without ASP.NET MVC

Open danieldegtyarev opened this issue 11 years ago • 2 comments

Is it possible using React.NET?

danieldegtyarev avatar Oct 22 '15 12:10 danieldegtyarev

Sure! See HtmlHelperExtensions.cs to see how it's hooked up in ASP.NET MVC. You could just call the same methods some other way. In 0dbd639efeb1387b1b82850667953e8fd7e441e7 I added an example showing how to render a React component from within a console application. There's a bit more code than I'd like, but it does work.

Basically, this is the code you need for server-side rendering:

var environment = React.AssemblyRegistration.Container.Resolve<IReactEnvironment>();
var component = environment.CreateComponent("HelloWorld", new { name = "Daniel" });
var html = component.RenderHtml();

You can then use environment.GetInitJavaScript() to get the JavaScript required to initialise all the instantiated components client-side. IReactEnvironment should be a per-request singleton (that is, every request creates a new one, but that same instance is shared throughout the entire request).

Hope that helps :)

Daniel15 avatar Oct 24 '15 04:10 Daniel15

Many thanx for the detailed answer, Daniel! I haven't tried yet, but it looks pretty helpful.

danieldegtyarev avatar Oct 24 '15 09:10 danieldegtyarev