Install ASP.NET Core and TypeScript

Next, if your version of Visual Studio does not already have the latest TypeScript, you can install it.

Create a new project

  1. Choose File
  2. Choose New Project (Ctrl + Shift + N)
  3. Select ASP.NET Core Web Application and press the Next button

  1. Name your project and solution. After select the Create button

Visual Studio New Project Window Screenshot

  1. In the last window, select the Empty template and press the Create button

Run the application and make sure that it works.

A screenshot of Edge showing "Hello World" as success

Open Dependencies > Manage NuGet Packages > Browse. Search and install and Microsoft.TypeScript.MSBuild:

Open up your Startup.cs file and edit your Configure function to look like this:

You may need to restart VS for the red squiggly lines below UseDefaultFiles and UseStaticFiles to disappear.

Next we will add a new folder and call it scripts.

The Path of "Add" then "New Folder" in Visual Studio from a Web Project

Add TypeScript code

Right click on scripts and click New Item. Then choose TypeScript File and name the file app.ts

Add example code

Add the following code to the app.ts file.

  1. ts
    function sayHello() {
    const compiler = (document.getElementById("compiler") as HTMLInputElement)
    .value;
    const framework = (document.getElementById("framework") as HTMLInputElement)
    .value;
    return `Hello from ${compiler} and ${framework}!`;
    }

Set up the build

Configure the TypeScript compiler

First we need to tell TypeScript how to build. Right click on scripts and click New Item. Then choose TypeScript Configuration File and use the default name of tsconfig.json

A screenshot showing the new file diaglogue with TypeScript JSON Config selected

Replace the contents of the tsconfig.json file with:

  • noEmitOnError : Do not emit outputs if any errors were reported.
  • noImplicitAny : Raise error on expressions and declarations with an implied type.
  • sourceMap : Generates corresponding .map file.
  • target : Specify ECMAScript target version.

Note: "ESNext" targets latest supported

"noImplicitAny" is good idea whenever you’re writing new code — you can make sure that you don’t write any untyped code by mistake. "compileOnSave" makes it easy to update your code in a running web app.

Set up NPM

We need to setup NPM so that JavaScript packages can be downloaded. Right click on the project and select New Item. Then choose NPM Configuration File and use the default name of package.json.

Inside the "devDependencies" section of the package.json file, add gulp and del

  1. "devDependencies": {
    "gulp": "4.0.2",
    "del": "5.1.0"
    }

After you should see an npm folder in your solution explorer

Screenshot of VS showing npm folder

Set up gulp

Right click on the project and click New Item. Then choose JavaScript File and use the name of gulpfile.js

The first line tells Visual Studio to run the task ‘default’ after the build finishes. It will also run the ‘clean’ task when you ask Visual Studio to clean the build.

Now right-click on gulpfile.js and click Task Runner Explorer.

If ‘default’ and ‘clean’ tasks don’t show up, refresh the explorer:

Screenshot of task explorer with "Gulpfile.js" in it

Right click on the wwwroot folder (if you don’t see the folder try building the project) and add a New Item named index.html inside. Use the following code for

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <script src="scripts/app.js"></script>
  6. </head>
  7. <body>
  8. <div id="message"></div>
  9. <div>
  10. Compiler: <input id="compiler" value="TypeScript" onkeyup="document.getElementById('message').innerText = sayHello()" /><br />
  11. Framework: <input id="framework" value="ASP.NET" onkeyup="document.getElementById('message').innerText = sayHello()" />
  12. </div>
  13. </body>
  14. </html>

Test

  1. Run the project
  2. As you type on the boxes you should see the message appear/change!

Debug

  1. In Edge, press F12 and click the Debugger tab.
  2. Look in the first localhost folder, then scripts/app.ts
  3. Put a breakpoint on the line with return.

An image showing the debugger running the code you have just wrote

Congrats you’ve built your own .NET Core project with a TypeScript frontend.