Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server-side rendering without ASP.NET MVC #178

Closed
danieldegtyarev opened this issue Oct 22, 2015 · 2 comments
Closed

Server-side rendering without ASP.NET MVC #178

danieldegtyarev opened this issue Oct 22, 2015 · 2 comments

Comments

@danieldegtyarev
Copy link

Is it possible using React.NET?

Daniel15 added a commit that referenced this issue Oct 24, 2015
This example shows how ReactJS.NET can be used outside of a web context. References #178
@Daniel15
Copy link
Member

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 0dbd639 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 :)

@danieldegtyarev
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants