How to deploy ASP.NET MVC applications to AppHarbor

I’ve already blogged about how awesome AppHarbor is.

So to back up my opinion I’ve decided to write a tutorial about how to deploy an ASP.NET MVC application to AppHarbor.

The application we’ll be deploying will be the ASP.NET MVC Music Store (created by James Senior, Jon Galloway and Scott Hanselman) which uses ASP.NET MVC 3 with Razor syntax and data access via Entity Framework 4.

ASP.NET MVC MusicStore Homepage

Click here to a deployed version of the ASP.NET MVC Music Store running on AppHarbor.

To follow along with the tutorial you’ll need to have the following installed:

If you’re also going to use the ASP.NET MVC Music Store please ensure you change the ..\Views\Shared\_Layout.cshtml to include HTML to indicate the site is a sample available from codeplex and a noindex robots meta tag.

<!DOCTYPE html>
<html>
<head>
    <meta name="robots" content="noindex,nofollow" />
    <title>This is a ASP.NET MVC sample application which can be downloaded at http://mvcmusicstore.codeplex.com</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet"
        type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")"
        type="text/javascript"></script>
</head>
<body>
 <p style="padding:20px;background-color:#000;color:#FFF;text-align:center">
        This is a ASP.NET MVC sample application which can be downloaded at <a href="http://mvcmusicstore.codeplex.com">http://mvcmusicstore.codeplex.com</a>
    </p>
    <div id="header">
        <h1>
            <a href="/">ASP.NET MVC MUSIC STORE</a>
        </h1>
       <ul id="navlist">
           <li class="first"><a href="@Url.Content("~")" id="current">Home</a></li>
           <li><a href="@Url.Content("~/Store/")">Store</a></li>
           <li>@{Html.RenderAction("CartSummary", "ShoppingCart");}</li>
           <li><a href="@Url.Content("~/StoreManager/")">Admin</a></li>
       </ul>
    </div>
    @{Html.RenderAction("GenreMenu", "Store");}
    <div id="main">
        @RenderBody()
    </div>
    <div id="footer">
        built with <a href="http://asp.net/mvc">ASP.NET MVC 3</a>
    </div>
</body>
</html>

Let’s get started

  1. How to add a ASP.NET MVC solution to a Git repository
  2. How to push a ASP.NET MVC Git repository to AppHarbor
  3. How To Create a SQL Server Database in AppHarbor
  4. How to configure ASP.NET MVC to use a SQL Server database in AppHarbor

I can browse around the store and add things to my cart but can’t log in as an administrator

That’s because you can only have one database per application… something I didn’t know when I started this series.

As AppHarbor doesn’t support file storage (yet) you can’t use the ASPDBNET.MDF database which the music store uses for ASP.NET Membership. See this AppHarbor support entry for more details.

Only One AppHarbor Database Is Allowed



You can of course workaround this by adding membership tables etc. to the existing SQL Server database by via an alternative membership provider which includes using another application hosted on AppHarbor.

How to add a ASP.NET MVC solution to a Git repository

In this tutorial we’ll be looking at how to add a ASP.NET MVC solution to a Git repository.

This tutorial is part of a series about how to deploy ASP.NET MVC applications to AppHarbor.

If you haven’t installed Git yet use this guide to get you started.

Before you initialize you git repository it’s a good idea to have a .gitignore file in root of the repository which instructs Git what files to ignore.

As you can see in the example below it excludes things which you don’t need to version control such as output directories and anything created by ReSharper.

*git
#ignore thumbnails created by windows
Thumbs.db
#Ignore files build by Visual Studio
*.obj
*.exe
*.pdb
*.user
*.aps
*.pch
*.vspscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.cache
*.ilk
*.log
[Bb]in
[Dd]ebug*/
*.lib
*.sbr
obj/
[Rr]elease*/
_ReSharper*/
[Tt]est[Rr]esult*

In the directory containing your ASP.NET MVC solution, right click and start Git Bash, or start Git Bash and navigate to the directory.

Git Directory Location

If this is the first time you’re using Git I recommended executing the following commands to set your user name and email address in the git.config which are used to track commits in the Git repository.

git config --global user.name jagreehal
git config --global user.email jag.reehal@gmail.com

To initialise a repository execute the following command

git init

Because we’ve used a gitignore file we can use the following Git command to add all files in the directory (and sub directories) into the repository.

git add .

Alternatively you could have added each file individually like this (for those who want total control)

git add <filename>

The final step is to commit your changes. The –m option allows you to add a message (otherwise you’ll be promoted for one)

git commit -m "Initial commit"

In the next part of the series we’ll be looking at how to push your ASP.NET MVC Git repository to AppHarbor.

How to push a ASP.NET MVC Git repository to AppHarbor

In this tutorial I’ll be showing you how to push a Git repository for a .NET solution site to AppHarbor.

This follows on from the tutorial for adding a ASP.NET MVC solution to a Git repository.

The first steps are to create an AppHarbor account if you haven’t got one, and then create an application

Create AppHarbor App



on the following page keep a note of the URLs where you’ll be pushing your Git repository to

App Harbor URLs



In the directory containing your ASP.NET MVC solution start Git Bash, or start Git Bash and navigate to the directory.

Git Directory Location To Push



Execute the following commands (substituting your URLs) to push your ASP.NET MVC solution to AppHarbor.

git remote add appharbor https://jagreehal@appharbor.com/MvcMusicStoreDemo.git
git push appharbor master

When you’re prompted to enter your password, use the same one for your AppHarbor account.

Next go to the build page on AppHarbor to see the status of your app.

App Harbor Build



If you’re following along with the example to deploy the ASP.NET MVC Music Store to AppHarbor you’ll see the site doesn’t work yet.

That’s because you can’t use MDF database files in AppHarbor yet so the next step is create the ASP.NET MVC Music Store SQL Server database in AppHarbor.

Page 1 of 212