I am trying to integrate an Angular 5 app generated with CLI project into an existing ASP.NET MVC 5 application. I created the angular project with CLI inside the ASP.NET MVC project, and I want to use Angular 5 components inside razor views; those will be simple razor views and inside those, I will display some Angular 5 components. I've done this with ASP.NET Core 2, I changed the Outdir
property to wwwroot
in the file .angular-cli.json
, and it worked, but I could not do it with ASP.NET MVC 5. Can anyone help? Thank you
PS: many tutorials use old versions of Angular and they used a file named system.js
, but this file does not exist anymore in the newer versions of Angular.
I just did this with Angular 5 and MVC 5. I suggest you get a "Hello World" working first, and then port it into your existing MVC problem.
Pre-reqs - make sure you have the latest Node, NPM, AngularCLI installed...
In VS2017, create a New ASP.NET Web Application, SPA with MVC and WebAPI 1.1. Show all files (in the Solution Explorer - this makes things easier!)
From the package manager console uninstall unneeded packages:
Remove all bundles in
BundleConfig.cs
(we'll pull them in later!)Delete Scripts folder (it will be recreated using the AngularCLI)
Add Angular using CLI (go into the project root folder)
Run:
ng new Scripts --skip-tests --style scss
In the Project, Include all folders EXCEPT
node_modules
!Update
angular.json
to output to aBundles
folder ->"outputPath": "../Bundles"
Add bundles:
(Optional) Re-add libraries through NPM (command prompt from Scripts folder):
(Optional) Reference them in angular.json so AngularCLI pulls them into the webpack bundles:
Build from scripts folder:
ng build --extractCss
In
_Layout.cshtml
, add:and
Remove the old bundle imports!
In
Home/Index.cshtml
, add the Angular element:13.1 Remove the old bundle imports!
Run the web app - it should open up. Register a new user, and then login. Show the MVC5 demo page together with the Angular
To "watch" the webpage - from the command line:
This will rebuild the Bundles. Unfortunately you need to refresh the browser, but this is better than stop/starting things!
Shout if you need more detail - I've run through this twice to get familiar with it, and it works nicely!
Solution and how to do it using Angular 4.3 here:
Have you seen the release candidate for the new Microsoft Core Angular template. They changed the template so that it uses angular-cli. Works well. You could possibly follow the same build config pattern. Note you have to install the newest version of the template via
dotnet new --install Microsoft.DotNet.Web.Spa.ProjectTemplates::2.0.0-rc1-final.
https://docs.microsoft.com/en-us/aspnet/core/spa/angular?tabs=visual-studio#tabpanel_IjUmMTId-R_visual-studio