Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use TypeScript in ASP.NET MVC

How to effectively use TypeScript with ASP.NET MVC?

I want my IDE to do the following actions when I run my project:

  1. Compile (interpret) my typescript files (*.ts) by typescript compiler.
  2. Minimize all the javascript files.
  3. Obviously run my app :).

How to configure IDE to do all the steps automatically on startup?

like image 489
Andrei Avatar asked May 02 '13 09:05

Andrei


People also ask

Can we use TypeScript in ASP NET MVC?

Here, we will learn about integrating TypeScript in ASP.NET MVC 5. TypeScript can be integrated into any existing ASP.NET application easily. TypeScript is one of the fastest-growing and highly adopted programming languages. You can learn the basic introduction and how to setup typescript from my previous articles.

Can I use TypeScript in asp net core?

Create an ASP.NET Core project. Add the NuGet package for TypeScript support. Add some TypeScript code. Run the app.

Can you use TypeScript with C#?

TypeScript is a popular choice for programmers accustomed to other languages with static typing, such as C# and Java. TypeScript's type system offers many of the same benefits, such as better code completion, earlier detection of errors, and clearer communication between parts of your program.

Can I use Visual Studio for TypeScript?

For projects developed in Visual Studio 2022, we encourage you to use the TypeScript NuGet or the TypeScript npm package for greater portability across different platforms and environments. For more information, see Compile TypeScript code using NuGet and Compile TypeScript code using tsc.


1 Answers

At this address, you will find that the current preview version of TypeScript is not providing a project template for ASP.NET MVC 4 projects. But you can edit your csproject file with the following code:

<ItemGroup>

    <TypeScriptCompile Include="$(ProjectDir)\**\*.ts" />

</ItemGroup>

  <Target Name="BeforeBuild">

   <Exec Command="&amp;quot;$(PROGRAMFILES)\

   Microsoft SDKs\TypeScript\0.8.0.0\tsc&amp;quot; 

  @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />

  </Target>

It allows you to compile all TypeScript when building ASP.NET MVC 4 project.

About the optimizing :

I suggest you separate the JavaScript files of your project and add references them into your master page like at this post : How can I automatically compress and minimize JavaScript files in an ASP.NET MVC app?.

You can also use the "bundling" with the Razor view. Personally I use that and it works perfectly.

like image 51
Francois Borgies Avatar answered Sep 20 '22 23:09

Francois Borgies