Using TypeScript in ASP.NET MVC Projects
Posted
by shiju
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by shiju
Published on Tue, 02 Oct 2012 16:45:44 GMT
Indexed on
2012/10/02
21:38 UTC
Read the original article
Hit count: 1989
In the previous blog post Microsoft TypeScript : A Typed Superset of JavaScript, I have given a brief introduction on TypeScript. In this post, I will demonstrate how to use TypeScript with ASP.NET MVC projects and how we can compile TypeScript within the ASP.NET MVC projects.
Using TypeScript with ASP.NET MVC 3 Projects
The Visual Studio plug-in for TypeScript provides an ASP.NET MVC 3 project template for TypeScript that lets you to compile TypeScript from the Visual Studio. The following screen shot shows the TypeScript template for ASP.NET MVC 3 project
The “TypeScript Internet Application” template is just a ASP.NET MVC 3 internet application project template which will allows to compile TypeScript programs to JavaScript when you are building your ASP.NET MVC projects. This project template will have the following section in the .csproject file
<None Include="Scripts\jquery.d.ts" />
<TypeScriptCompile Include="Scripts\site.ts" />
<Content Include="Scripts\site.js">
<DependentUpon>site.ts</DependentUpon>
</Content>
<Target Name="BeforeBuild">
<Exec
Command="&quot;$(PROGRAMFILES)\
Microsoft SDKs\TypeScript\0.8.0.0\tsc&quot;
@(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
</Target>
The “BeforeBuild” target will allows you to compile TypeScript programs when you are building your ASP.NET MVC projects. The TypeScript project template will provide a typing reference file for the jQuery library named “jquery.d.ts”. The following default app.ts file referenced to jquery.d.ts
1: ///<reference path='jquery.d.ts' />
2:
3: $(document).ready(function () {
4:
5: $(".btn-slide").click(function () {
6: $("#main").slideToggle("slow");
7: $(this).toggleClass("active");
8: });
9:
10: });
Using TypeScript with ASP.NET MVC 4 Projects
The current preview version of TypeScript is not providing a project template for ASP.NET MVC 4 projects. But you can use TypeScript with ASP.NET MVC 4 projects by editing the project’s .csproject file. You can take the necessary settings from ASP.NET MVC 3 project file. I have just added the following section in the end of the .csproj file of a ASP.NET MVC 4 project, which will allows to compile all TypeScript when building ASP.NET MVC 4 project.
<ItemGroup>
<TypeScriptCompile Include="$(ProjectDir)\**\*.ts" />
</ItemGroup>
<Target Name="BeforeBuild">
<Exec Command="&quot;$(PROGRAMFILES)\
Microsoft SDKs\TypeScript\0.8.0.0\tsc&quot;
@(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
</Target>
© ASP.net Weblogs or respective owner