HI WELCOME TO SIRIS

ASP.NET MVC 5 – Using Angular 4 with TypeScript in Visual Studio

Leave a Comment
These steps can be used for new or existing MVC 5 application.
In this post, I will summarize the steps needed to getting started with Angular 2 in MVC 5.
ASP.NET MVC 5 is full .NET framework web development framework, it’s different from ASP.NET Core 1.0
What will we learn?
  1. Adding package.json to MVC 5
  2. Configure to transpile TypeScript files
  3. Using gulpfile.js to move files.
  4. Add TypeScript files for bootstrapping
  5. Include systemjs.config.js to load Angular 2 modules
  6. Change HTML to load and render Angular

Step 1: Adding package.json to ASP.NET MVC 5

Assuming that you already have existing or created new ASP.NET MVC 5. Let’s add NPM configuration file known as package.json. It contains Angular 4 (works for Angular 2) & related package name to installed using NPM (Node). This is similar to package.config of NuGet.
Latest NodeJS & NPM needs to be installed.
package.json contains Angular 2 (using version 4) along with, system.js, RxJs and also some dev dependencies.
Open Command Prompt & navigate to package.json location, then run npm install this will install packages related to Angular 2 (using version 4) under node_modules folder in your folder structure.
They won’t be showing in project solution explorer, don’t worry they need not show.

Step 2: Configure to transpile TypeScript files

TypeScript(TS) would be new for most of the developers, maybe these will give get started on TypeScript
In short – It’s superset of JavaScript, means everything you know about JS will be in use.
All TS files need to be transpiled or compiled to JS files so that we can run them on browser. To accomplish this we need to add “TypeScript Configuration File” called as tsconfig.json
Create a folder called “tsScripts” which contains all TS files and also configuration file. Create above tsconfig.json in this folder.
It’s fairly simple config “All TS files present in tsScripts folder will be transpiled using commonjs module to outDir(Output Directory) by keeping comments, sourceMap intact”
One of the important step is to create typings.json file, this file will create typings to ensure that TypeScript understands all Angular 2 (using version 4) modules in respect to ES5 standard.
Create JSON file with name “typings.json” & add below code then run command typings install from CMD

Step 3: Using gulpfile.js to move files

From Step 1 you got to know that all Angular 2, other packages are downloaded into node_modules in your solution folder. Now we need to move required files only like JS, sourcemaps (debugging on chrome) into our MVC 5 apps Scripts folder.
Step 2 also requires to move TS files to JavaScript file, so we use create GULP tasks which does this transpile. Also it contains CSS files movement also (which is not relevant at this point)
Create gulpfile.js in your project and copy this code

Step 4: Add TypeScript files for bootstrapping

As we are using Angular 2 with TypeScript, we need to add TS file to bootstrap the Angular app into MVC 5 app. So let’s create boot.ts file in “TsScripts” folder and copy this code.
We using ngModule to browser specifics, AppComponent.
AppComponent is starter component which we have to create it now (app.ts). Add this code below
This is template based app component which displays list.
Now create tsScripts/main.ts, this entry point where Angular 2 loads the components.

Step 5: Include systemjs.config.js to load modules

This is most important part of Angular 2 (using version 4) which loads it into the browser. There are different ways to load it but am using SystemJS here.
In the existing “Scripts” folder, create “systemjs.config.js” and copy below code

Step 6: Change csHTML to load and render Angular 4

To load Angular 2 in ASP.NET MVC 5, we need to include the script references in _Layout file and index.cshtml page.
In the Views/Home/index.cshtml you need to add “my-app” component we defined in app.TS file. This is the starting point of Angular 2 application to render into the browser.
Now that we are almost done, we need to run GULP tasks so that Angular 2 files, TS files are moved to an appropriate folder. Open Task Runner Explorer in Visual Studio 2015 and run default task shown.
Its better to do show ALL Files in solution Explorer to see “libs“, “typings“, “app.js, boot.js, *.map” files. Includes these files and run the application to load Angular 2 in ASP.NET MVC 5.
ASP.NET MVC 5 Angular 2
Running Angular 2 in ASP.NET MVC 5
The entire source code is on my Github repo, clone or fork or download it and follow instructions to run it.
Let me know in comments if you face any issues running applications.

With Angular CLI

Check out this post to use Angular CLI to integrate Angular with ASP.NET MVC 5

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.