ASP.NET C# Heroku MVC Core

Deploying ASP.NET Core to Heroku

Heroku is a great Infrastructure as a Service platform that provides easy deployment for Node, Ruby, PHP, Go, and many others – unfortunately, this does not include ASP.NET out-of-the-box. This is because ASP.NET has been a Windows-only platform since its inception, and Heroku only supports Linux. BUT, ASP.NET Core is out (yaaaay) and we can now run our .NET apps on Linux! Still no direct Heroku supoort, but here’s how to get up-and-running.

1. Create the Heroku app using a ASP.NET Core buildpack.

This ASP.NET Core Buildpack worked great for me. Just follow its steps in the Usage section:

$ heroku create --buildpack http://github.com/noliar/dotnet-buildpack.git
$ git push heroku master

2. Create your project in the root of the Git repo or set the config to point at the correct project.

If you don’t want to use the root of the repo, follow these steps to use a .deployment file to point at the appropriate folder.

3. Set the Microsoft.NETCore.App version in project.json to “1.0.0”

Setting version 1.0.1 will not work with this buildpack, so you will need to downgrade to 1.0.0.

4. Set up the Program.Main() method to use the PORT environment variable.

Heroku requires that whatever application you use is connected to a port specified by Heroku in the environment variables. This can be configuredd in the Program.cs file by changing the Main method to the following:

public static void Main(string[] args)
{
    var url = $"http://*:{Environment.GetEnvironmentVariable("PORT")}/";

    Console.WriteLine($"Using Url: {url}");

    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .UseUrls(url)
        .Build();

    host.Run();
}

5. Push to heroku master

That’s it! You should be up and running.

Leave a Reply

Your email address will not be published. Required fields are marked *