@TheDull
WHO ARE YOU
AND
WHY ARE YOU TALKING TO US HERE TODAY?
ASP.NET Core first + then scaffolding generator using framework-specific CLI
Server Side Rendering (How to run JS in .NET?)
Wait for some merciful soul to port it to NuGet or write it yourself.
Try not to cry, cry, cry more...
Different approaches:
😥
😰
☠️
Say "hello" to my little friend
Microsoft.AspNetCore.SpaTemplates
Microsoft.AspNetCore.SpaServices
Microsoft.AspNetCore.NodeServices
Requirements:
ASP.NET Core 1.0 RC4 +
NodeJS 6+
After installing Microsoft.AspNetCore.SpaTemplates:
Out of the box:
Set an environment variable to tell ASP.NET to run in development mode
mkdir <project-dir> && cd $_
dotnet new <spa-fx>
dotnet restore
npm install
dotnet run
Application will be available by default at: http://localhost:5000
1️⃣
2️⃣
3️⃣
4️⃣
5️⃣
6️⃣
Hot module replacing
Efficient production builds
1. Enable the asp-prerender-* tag helpers
2. Use asp-prerender-* in a view
3. Supply JavaScript code to perform prerendering
// In ./ClientApp/boot-server.js
var prerendering = require('aspnet-prerendering');
module.exports = prerendering.createServerRenderer(function(params) {
return new Promise(function (resolve, reject) {
var result = '<h1>Hello world!</h1>'
+ '<p>Current time in Node is: ' + new Date() + '</p>'
+ '<p>Request path is: ' + params.location.path + '</p>'
+ '<p>Absolute URL is: ' + params.absoluteUrl + '</p>';
resolve({ html: result });
});
});
// In Views/_ViewImports.cshtml
@addTagHelper "*, Microsoft.AspNetCore.SpaServices"
// In a view (e.g. Views/Home/Index.cshtml)
<div id="my-spa" asp-prerender-module="ClientApp/boot-server"></div>
Passing data from .NET code into JavaScript code
<!-- In Views/Home/Index.cshtml -->
<div id="my-spa" asp-prerender-module="ClientApp/boot-server"
asp-prerender-data="new {
IsGoldUser = true,
Cookies = ViewContext.HttpContext.Request.Cookies
}"></div>
// In ./ClientApp/boot-server.js
var prerendering = require('aspnet-prerendering');
module.exports = prerendering.createServerRenderer(function(params) {
return new Promise(function (resolve, reject) {
var result = '<h1>Hello world!</h1>'
+ '<p>Is gold user: ' + params.data.isGoldUser + '</p>'
+ '<p>Number of cookies: ' + params.data.cookies.length + '</p>';
resolve({ html: result });
});
});
Passing data from server-side to client-side code
// In ./ClientApp/boot-server.js
...
resolve({
html: result,
globals: {
albumsList: someDataHere,
userData: someMoreDataHere
}
});
And global variables window.albumList and window.userData will be available at your will.
It lets you work as if the browser natively understands whatever file types you are working with (e.g., TypeScript, SASS), because it's as if there's no build process to wait for.
It intercepts requests that would match files built by webpack, and dynamically builds those files on demand. They don't need to be written to disk - they are just held in memory and served directly to the browser.
No need to run webpack manually or set up any file watchers.
The browser is always guaranteed to receive up-to-date built output.
The built artifacts are normally served instantly or at least extremely quickly, because internally, an instance of webpack stays active and has partial compilation states pre-cached in memory
npm install webpack-hot-middleware
HMR != Live reload
Development
Source maps
.ts
.vue
.jsx
.tsx
[ ]
Production
In browser
On server
V8 Inspector Integration:
// In Startup.cs file,
// in the ConfigureServices method
...
services.AddNodeServices(options => {
options.LaunchWithDebugging = true;
options.DebuggingPort = 9229;
});
dotnet run
1)
2)
Open URL provided by console in Chrome
3)
Open webpack://<<your-original-source file> and set breakpoints
4)
Re-run in another browser window/tab, and the breakpoints will be hit
5)
Install the Microsoft.AspNetCore.SpaServices NuGet package
Run dotnet restore
Install supporting NPM packages for the features you'll be using:
Server-side prerendering: aspnet-prerendering
Server-side prerendering with Webpack build support: aspnet-webpack
Webpack dev middleware: aspnet-webpack
Webpack dev middleware with hot module replacement: webpack-hot-middleware
Webpack dev middleware with React hot module replacement: aspnet-webpack-react
The real magic behind curtains
Add Microsoft.AspNetCore.NodeServices to the project dependencies
dotnet restore
Enable NodeServices in your application by first adding it to your ConfigureServices method in Startup.cs
After that, you can receive an instance of NodeServices as an action method parameter to any MVC action, and then use it to make calls into Node.js code
Finally, create a module that will perform the actual interaction between ASP.NET and NodeJS, notice the usage of the callback.
1️⃣
2️⃣
3️⃣
4️⃣
5️⃣
Say "hello" to my little friend