Book Image

ASP.NET Core 2 and Vue.js

By : Stuart Ratcliffe
5 (1)
Book Image

ASP.NET Core 2 and Vue.js

5 (1)
By: Stuart Ratcliffe

Overview of this book

This book will walk you through the process of developing an e-commerce application from start to finish, utilizing an ASP.NET Core web API and Vue.js Single-Page Application (SPA) frontend. We will build the application using a featureslice approach, whereby in each chapter we will add the required frontend and backend changes to complete an entire feature. In the early chapters, we’ll keep things fairly simple to get you started, but by the end of the book, you’ll be utilizing some advanced concepts, such as server-side rendering and continuous integration and deployment. You will learn how to set up and configure a modern development environment for building ASP.NET Core web APIs and Vue.js SPA frontends.You will also learn about how ASP.NET Core differs from its predecessors, and how we can utilize those changes to our benefit. Finally, you will learn the fundamentals of building modern frontend applications using Vue.js, as well as some of the more advanced concepts, which can help make you more productive in your own applications in the future.
Table of Contents (15 chapters)

Enabling SSR

We finally have all of the configuration files in place that we'll need in order to render our app on the server. We now need a file that will be invoked directly from our MVC view and will be responsible for actually rendering the application for us. Create a new ClientApp/renderOnServer.js file, then start it off with the following:

process.env.VUE_ENV = "server";

const fs = require("fs");
const path = require("path");

const filePath = path.join(__dirname, "../wwwroot/dist/main-server.js");
const code = fs.readFileSync(filePath, "utf8");

const bundleRenderer = require("vue-server-renderer").createBundleRenderer(
code
);

The important part here is how we physically read the wwwroot/dist/main-server.js file, then create a bundle renderer using the contents of it. The main-server.js file is the product of...