Use Angular with existing MVC projects, using Angular CLI.
I wanted to use Angular in my existing MVC project. Most options around were to use Angular as its own and as SPA application. Following steps will let you set up an Angular app inside existing Asp core MVC project. Then you can use this app on any existing or new View.
- NodeJS and npm.
- Following example is in accordance with dotnetcore MVC Application created in Visual Studio 2017.
This repository is created using Angular CLI.
Please follow Angular CLI to create new application.
Othewise just clone this repository to a folder with project name e-g my-angular-app, and follow next steps.
git clone https://github.com/AhsanRazaUK/AngularWithMVC my-angular-app
cd my-angular-app
- Open existing Solution or create new project in Visual Studio where you want to use Angular app
- Copy all files/folders of
my-angular-app - Right Click on to your project in Visual Stuio and paste these files in project
- Click
Show All Filesfrom top of Solution Explorer to verify eithernode_modulesfolder has been created - Open
_Layout.cshtml. And add following tags to call Angular script files inenvironmenttag above@RenderSection("Scripts", required: false)inbodysection.
<script src="~/dist/AngularApp/runtime.js"></script>
<script src="~/dist/AngularApp/polyfills.js"></script>
<script src="~/dist/AngularApp/styles.js"></script>
<script src="~/dist/AngularApp/vendor.js"></script>
<script src="~/dist/AngularApp/main.js"></script>
body section must look like this now
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© 2018 - AngularMVCCore</p>
</footer>
</div>
<environment include="Development">
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
<script src="~/dist/AngularApp/runtime.js"></script>
<script src="~/dist/AngularApp/polyfills.js"></script>
<script src="~/dist/AngularApp/styles.js"></script>
<script src="~/dist/AngularApp/vendor.js"></script>
<script src="~/dist/AngularApp/main.js"></script>
</environment>
- Open View
.cshtmlfile where you want to use Angular. e-gAbout.cshtmland replce code with<app-root></app-root>there.
@{
ViewData["Title"] = "About";
}
<h2>@ViewData["Title"]</h2>
<h3>@ViewData["Message"]</h3>
<app-root></app-root>
- Right Click on
.csprojfile, from context menu chooseEdit ....csproj. In openedXMLadd following
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
</PropertyGroup>
- Right-Click on
package.jsonfile andRestore Packages - Open
Command Promptand go to folder where.csprojresides. And runng build - If above runs successfully add
ng buildtoBuild Eventsof the project inpre build event command line :area - Rebuild and Run the application and you must see Angular component loaded in your
View(About). - Press
F12in browser and clickconsole, you may see this message.
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
- Ahsan Raza
This project is licensed under the MIT License - see the LICENSE.md file for details