Installing Angular into the project
Creation of a new Angular project inside the folder of our Visual Studio Solution where it belongs.
- Open Command Prompt (cmd)
- Go to the root directory where the <projectname>.csproj is located
- Type: ng new temp-app –minimal
- Go into the newly created ‘temp-app’ folder
- Type: ng build
- Use ng serve to check if everything is working
- Close the Command Prompt
- Open the Visual Studio solution and check if the newly created folder ‘temp-app’ is included
Copying the Angular project
Integration of the Angular project in the APS. NET (Core) MVC project.
- In Visual Studio, right-click the project and click ‘Open Folder in File Explorer’
- Create a new folder called ‘ClientApp’
- Go to the ‘temp-app’ folder and cut everything inside from the ‘src’ folder and paste it into the ‘ClientApp’ folder
- Go back to the ‘temp-app’ folder and cut the files’.angular-cli.json’, ‘package.json’ and ‘tsconfig.json’
- Paste them at the root directory (overwrite any existing files)
- Delete the ‘temp-app’ folder
Setting up the Angular project
Modification of a few files so the Angular plays nicely together with the APS .NET (Core) MVC project.
- Open .angular-cli.json
- Change the value of ‘root’ to ClientApp
- Change the value of ‘outDir’ to ./Angular/ClientApp/dist
- Remove the line “index”: “index.html”
- Open the tsconfig.json file
- Change the value of ‘outDir’ to ./Angular/ClientApp/out-tsc
- Add the property ‘include’ with values: [ “./ClientApp”, “./Angular/ts” ]
- Save all changes and close Visual Studio
- In Command Prompt go to the root directory
- Type: npm install (if this fails add –force)
- Type: ng build
- Open the project and right-click the ClientApp and click “Add files to Source Control”
- Right-click the folder node-modules and click ‘Exclude from Project’
Adding the JavaScript files for Angular
Angular needs a few files to work correctly; we will add them to our APS.NET (Core) MVC project
In \Views\Shared\_Layout.cshtml add the following scripts in this order:
<script src="~/Angular/ClientApp/dist/inline.bundle.js"></script> <script src="~/Angular/ClientApp/dist/polyfills.bundle.js"></script> <script src="~/Angular/ClientApp/dist/styles.bundle.js"></script> <script src="~/Angular/ClientApp/dist/vendor.bundle.js"></script> <script src="~/Angular/ClientApp/dist/main.bundle.js"></script>
Optional – Build Angular when building the project
- Right-click the project
- Select ‘Properties’
- Select ‘Build Events’
- In ‘Pre-build event command line’ type:
- ng build