Getting Started

This getting started guide assumes that you have Visual Studio 2010, NuGet and Asp.net MVC 3 installed.

Creating the projects

To produce any usable output, you will need two projects. First create a new Asp.net MVC3 Web Application, call it WebProject. Use the Razor view engine and the Internet application template. Then create a regular console application project. Call it ScriptProject.

Make it a script project

Now it is time to transform the normal console application (ScriptProject) to a project that will compile to JavaScript. Open the package manager console, make sure that ScriptProject is selected as the default project and type

> Install-Package Saltarelle.Compiler

After this completes, your project uses the Saltarelle compiler whenever you build it (if you unload the project and open the .csproj you can see that the usual <Import Project="Microsoft.CSharp.targets"/> has been replaced with <Import Project="Saltarelle.Compiler.targets"/>). During installation it also removed all existing references since those will not work anymore. Note: This will set the target framework to 2.0. Do not change the target framework.

If you try to compile it, you will get a lot of errors complaining about predefined types not being defined. That is because we haven't yet added a reference to our runtime library. To do that, go to the package manager console and type

> Install-Package Saltarelle.Runtime

This will add a reference to the Saltarelle custom mscorlib to your project. It works immediately, but due to a Visual Studio bug you need to unload and reload the project before the reference is visible in the UI. If you try to compile it now you will have a lot fewer errors, all of them concerning things that don't exist in the Saltarelle runtime (namespaces System.Text and System.Linq, and perhaps a few assembly attributes). Remove everything it complains about and compile. If you now go to the output folder (bin\Debug), you should see a file called ScriptProject.js. This is your compiled script, take a look at it!

So now we can compile our project, but we can't yet communicate with the outside world (aside from using 'dynamic' everywhere, but if we wanted to do that we would just write the script directly). To enable that we need to install the import library for the browser object model (in the package manager):

> Install-Package Saltarelle.Web

and since only fools write dynamic web sites without jQuery, we want that, too:

> Install-Package Saltarelle.jQuery

Now we have everything we need to start compiling to script. (and, yes, the dependencies are set up correctly so in the future you need just install the jQuery package, which will install everything else).

Making it actually do something

So, now that we can make it compile, we should be able to make it do something. This example will be very stupid and 100 times easier to to by just writing the script directly in the page, but this is just a getting started guide.

So, delete everything from Program.cs and paste in this instead:

using System;
using System.Collections.Generic;
using jQueryApi;

namespace ScriptProject {
    public class Program {
        static void Main() {
            jQuery.OnDocumentReady(() => {
                jQuery.Select("#main h2").Click(evt => jQuery.Select("#main p").FadeToggle());
            });
        }
    }
}

If you are using Visual Studio 2012, you can even try this:

using System;
using System.Collections.Generic;
using System.Html;
using jQueryApi;

namespace ScriptProject {
    public class Program {
        static void Main() {
            jQuery.OnDocumentReady(() => {
                jQuery.Select("#main h2").Click(async (el, evt) => {
                    await jQuery.Select("#main p").FadeOutTask();
                    await jQuery.Select("#main p").FadeInTask();
                    Window.Alert("Done");
                }
            });
        }
    }
}

After doing this, spend some time in the editor and hit Ctrl+space and all other shortcuts you love to use. They all work! (the only thing that doesn't, unfortunately, is that ReSharper 6 complains about all built-in types; this is fixed in R#7).

Now we want the script to be usable from the web application, so set up a post-build task (in the ScriptProject project):

copy "$(TargetDir)$(TargetName).js" "$(SolutionDir)\WebProject\Scripts"

If you feel really ambitious, you can also set up a project dependency so the WebProject depends on the ScriptProject, which will ensure that the file is always up to date.

Now it is time to turn to the web application. First, we need to include the runtime library, which we find at $(SolutionDir)\packages\Saltarelle.Runtime.x.y.z. It has both debug and release versions, for now we'll use the debug version so copy mscorlib.js to the Scripts folder in your web application project. While you're at it, also include ScriptProject.js in the project.

Now go to Views\Shared\_Layout.cshtml and insert these script includes:

<script src="@Url.Content("~/Scripts/mscorlib.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/ScriptProject.js")" type="text/javascript"></script>

Run the application, click the header and watch the text below it fade in and out.

Congratulations, you have just compiled your first C# to JavaScript!