Hello Angular World (from Visual Studio and Local IIS)
I have written this article and the accompanying Visual Studio 2015 project as an exploration of how Angular 2 and ASP.NET MVC 5 can coexist and I have tried to remain true to both frameworks, respecting naming conventions and file structure. While carrying out research for this article, I came across the Angular2Mvc5Application Project Template which did tick most of the boxes, however I wanted to know more about the inner workings, structure and naming conventions.
As you will see I have covered a lot of detail, so you may decide to download the source code to dip in and out as you need, or to read the article from end to end. Either way, the complete source code and project files can be found on GitHub: https://github.com/sionjlewis/Hello.Angular.World
The following topics are covered:
- NPM Task Runner & Package Installer
- Create a Visual Studio Project
- NPM Packages
- Restore NPM Packages
- Update the “scripts” Property (package.json)
- ‘tsconfig.json’ file
- Angular Folder & File Structure
- Hello Angular World from a HTML page
- Hosting with Local IIS
- Config Visual Studio Web Application
- Hello Angular World from a MVCV View
- References & Useful Links
Note: You may notice references to My.iTunes within the code, this is because I extracted this code from another solution with the sole purpose of keeping the example as simple as possible.
Before we begin, please make sure that you have ticked off the following prerequisites. The following list builds upon the Visual Studio 2015 Quickstart article published on the Angular website: https://angular.io/docs/ts/latest/cookbook/visual-studio-2015.html#!#prereq1
- Turn on the Internet Information Services Windows Feature.
This action is only required if you plan to develop against your Local IIS server, opposed to IIS Express which ships with Visual Studio.
- Download and install Node.js (and NPM) https://nodejs.org/en/download/
- Confirm that you have Visual Studio 2015 Update 3 installed:
- Open Visual Studio 2015 and click Help > About Visual Studio
- If Update 3 is not installed, then download and install: https://www.visualstudio.com/en-us/news/releasenotes/vs2015-update3-vs
- Configure Visual Studio to use the global external web tools, and not the tools that ship with Visual Studio:
- Open Visual Studio 2015 and click: Tools > Options
- Navigate to: select Projects and Solutions > External Web Tools
- Add the nodejs Program Files directory at the top of the Locations of external tools list. In my case this path is C:\Program Files\nodejs
Note: Visual Studio will now look first for external tools in the current workspace, if not found it will then look in the global path and if it is not found there, Visual Studio will use its own versions of the tools.
- Install TypeScript 2 for Visual Studio 2015:
- Either: Download and install TypeScript 2.0 for Visual Studio 2015
- Or: Open the Node.js Command Prompt and enter: npm install -g firstname.lastname@example.org
Note: TypeScript 2.0 is now available for Visual Studio https://blogs.msdn.microsoft.com/typescript/2016/09/22/announcing-typescript-2-0/
- Set Visual Studio 2015 to Run as Administrator
- Locate the short cut or favourite that you use to open Visual Studio
- Right-click and click Properties
- Click Shortcut > Advanced…
- Tick the Run as administer checkbox
- Click OK
Note: You may run into difficulties debugging without Run as administer, however depending on your UAC settings you are likely to see a User Access Control promote every time you open Visual Studio. Click Yes to continue to open Visual Studio.
- Restart Visual Studio
Note: It is probably a good idea to restart your machine after the next section
NPM Task Runner & Package Installer
Install the following Visual Studio Extensions (if not already installed):
- NPM Task Runner
- Package Installer
- Restart your machine
Note: I couldn’t find the Task Runner Explorer window at first, however after restarting my machine and some mucking around I found here: View > Other Windows > Task Runner Explorer (Ctrl+Alt+Bkspace)
Create a Visual Studio Project
At this point I am assuming you have completed the prerequisites and have Visual Studio 2015 open and ready to create a new project (and solution).
- Select: File > Add > New Project…
- Select: Visual C# > Web > ASP.NET Web Application (.NET Framework)
Note: At the time of writing this I had .NET Framework 4.6.1 installed
- Name: Hello.Angular.World
- Location: <select location>
- Solution name: Hello.Angular.World
- Tick Create directory for solution
- Click OK
- Select a template Empty and tick MVC
- Click OK
Your project should look something like this:
You must be logged in to post a comment.