Integrating Vue.js 2.0 to .NET MVC5 Project
Recently, I built a POC for PWA using Vue.js, Vuex, and Vuetify. I shared it with my colleagues and it was a blast I say. The demo I built was based on our existing MVC5 project which is responsive but not 100% mobile ready. We’ve been looking for a good solution that can cover both web and mobile and have come to an agreement that Vue.js and Vuetify could be our solution for future development.
After the session, some colleagues wanted to jump right on to Vue.js but there are some cases that we must consider before starting the actual development such as integration of Vue.js to .Net project, browser supports, and etc. For a small project we could start a new project using vue-cli separating all client code to vue.js project and convert existing mvc5 controllers to web apis. But for big projects we will have to include vue.js into existing projects and we had no idea whether this is even possible.
Thanks to google search, I could easily find some references about integrating Vue.js to .Net project as below.
Building Web apps with VueJS and .Net
But as always(not 100% but in most cases), our environments tend to defer from online cases that I cannot seamlessly follow the instructions(not blaming the original authors but I just needed detailed information to achieve the goal when I followed them). So I decided to provide a work through of how to integrate Vue.js 2.0 to .Net project from my own experiences and to be honest, this is more dedicated to my colleagues than general cases where my colleagues likely have higher chances having the same or similar dev environment as mine.
Create an ASP.NET Web Application project
We will start with a empty MVC project with following specification:
- Select Empty ASP.NET 4.5 Template.
- Select Add folders and core references for: MVC
Initialize Vue webpack bundle
Now that we have a clean starting point, open a terminal in the project folder.
Run below command to install vue-cli globally.
npm install -g vue-cli
The following command will walk you through creating a package.json file installing vue-webpack-boilerplate.
vue init webpack-simple
A full-featured Webpack setup with hot-reload, lint-on-save, unit testing & css extraction is provided by vue-cli webpack-simple tamplates.
Make sure package.json file is created in your project’s root folder as below.
Following command will start a dev server.
npm run dev
Now you are ready to code vue.js. But please hold on as we are here to integrate vue.js to MVC5.
Our existing mvc project has areas and we need to implement different components for each area. Consider that we have two areas named ‘first’ and ‘second’, we will have to write vue.js app named ‘first’ and ‘second’ as below.
Now we need to modify default webpack.config.js to change entry point and output point. We will take first and second folder under Script/app to produce bundling a compiled javascript.
var path = require('path')var webpack = require('webpack')var fs = require('fs')var appBasePath = './Scripts/app/'var jsEntries = {}// We search for index.js files inside basePath folder and make those as entriesfs.readdirSync(appBasePath).forEach(function (name) { var indexFile = appBasePath + name + '/main.js' if (fs.existsSync(indexFile)) { jsEntries[name] = indexFile}})module.exports = {entry: jsEntries,output: { path: path.resolve(__dirname, './Scripts/bundle/'), publicPath: '/Scripts/bundle/', filename: '[name].js'},
...
Note that jsEntries array is filled with main.js folder names under Scripts/app/{areaname} folder and being entry point for the exporting.
Run below command to build our apps.
npm run build
Vue apps under Scripts/app will be compiled and bundled into Scripts/bundle with the respective names.
When you are ready with the project setup, run below command.
npm run build
This command will compile vue apps and generates bundles in Scripts/bundle folder. Include these bundles into your cshtml.
If you want hot-reloading, add the following config to webpack.config.js
Note the port number after the localhost should match to mvc project port.
Now you are ready to use hot-reloading. Try npm run dev and go to one of your controller url.
Now if you change your vue app, hot-reloading will detect your changes.
The sample code is located at my github repository.
Thanks for reading.