How to run Hello World with .NET

This tutorial describes how to deploy an ASP.NET application from a Windows development environment to a Google Compute Engine instance running Windows Server 2012 R2.

This tutorial assumes that you have Visual Studio 2013 or later installed and are familiar with the .NET Framework and the C# language.

This is part of the Getting Started with .NET on Google Cloud Platform series . After you complete the prerequisites listed under Before you begin, the tutorial takes about 15 minutes to complete.

Before you begin

Check off each step as you complete it.

  1. check_box_outline_blank check_box Create a project in the Google Cloud Platform Console.
    If you haven't already created a project, create one now. Projects enable you to manage all Google Cloud Platform resources for your app, including deployment, access control, billing, and services.
    1. Open the Cloud Platform Console.
    2. In the drop-down menu at the top, select Create a project.
    3. Click Show advanced options.
    4. Give your project a name.
    5. Make a note of the project ID, which might be different from the project name. The project ID is used in commands and in configurations.
  2. check_box_outline_blank check_box Enable billing for your project, and sign up for a free trial.

    If you haven't already enabled billing for your project, enable billing now, and sign up for a free trial. Enabling billing allows the application to consume billable resources such as running instances and storing data. During your free trial period, you won't be billed for any services.

Download and run the app

We've created a simple Hello World app using .NET so you can quickly get a feel for deploying an app to Google Cloud Platform. After you've completed the prerequisites, you can download and deploy the Hello World sample app. This section guides you through getting the code and running the application locally.

Get the Hello World app

Download the sample as a zip and extract it.

Alternatively, you can clone the git repository:

$ git clone https://github.com/GoogleCloudPlatform/getting-started-dotnet.git

Run the app on your local computer

  1. Double click aspnet\1-hello-world\1-hello-world.sln so it opens in Visual Studio 2015.

  2. In Visual Studio, press F5 to build and run the application.

You can see the Hello, World! message from the sample app displayed in the page. This page is delivered by a web server running on your computer.

When you are ready to move forward, press Shift-F5 to stop the application.

Hello World code review

The code is a simple ASP.NET application.

        /// <summary>
        /// The simplest possible HTTP Handler that just returns "Hello World."
        /// </summary>
        public class HelloWorldHandler : HttpMessageHandler
        {
            protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
                CancellationToken cancellationToken)
            {
                return Task.FromResult(new HttpResponseMessage()
                {
                    Content = new ByteArrayContent(Encoding.UTF8.GetBytes("Hello World."))
                });
            }
        };

        public static void Register(HttpConfiguration config)
        {
            var emptyDictionary = new HttpRouteValueDictionary();
            // Add our one HttpMessageHandler to the root path.
            config.Routes.MapHttpRoute("index", "", emptyDictionary, emptyDictionary,
                new HelloWorldHandler());
        }

Running Hello World on Google Cloud Platform

Create and configure a new Compute Engine instance

First, use the Google Cloud Launcher to create a new Compute Engine instance that has Windows Server 2012 R2, Microsoft IIS, ASP.NET, and SQL Express preinstalled:

  1. In the Cloud Platform Console, go to the Cloud Launcher ASP.NET Framework page.

  2. Set your deployment name and preferred Compute Engine zone.

  3. Click Deploy ASP.NET Framework to deploy the instance.

After the deployment process finishes, add a default Windows user to your new instance:

  1. In the Cloud Platform Console, go to the VM instances page.

  2. Click the name of your newly deployed instance. If you used the default deployment settings, the instance name will have the prefix aspnet.

  3. On the instance page, click Create or reset Windows password.

  4. In the Set new Windows password dialog, add your username, and click Set to create the user account on your instance.

  5. Make a note of the provided password, and close the dialog.

Deploy the application to your Windows instance

  1. In Visual Studio, in the Solution Explorer pane, right click your application.

  2. In the context menu, click Publish....

    Publish application

  3. In the Publish Web dialog, select Custom as your publish target.

  4. In the New Custom Profile dialog, provide a name for the deployment profile, and click OK.

  5. Fill out your profile as follows:

    Server
    The external IP address of your Compute Engine instance. This address can be found on the VM instances page in the Cloud Platform Console. The IP address is ephemeral, which is sufficient for our purposes.
    Site name
    Default Web Site
    User name
    The username of the Windows user account you created on your Compute Engine instance.
    Password
    The password of the Windows user account you created on your Compute Engine instance.
    Destination URL
    http://<external_ip_of_your_compute_instance>
  6. Click Validate Connection to ensure that the properties are correct.

  7. Because the Microsoft IIS installation in your deployment uses a self-signed certificate by default, you'll see a Certificate Error during the validation process. Check the box to Save this certificate for future sessions of Visual Studio, and click Accept to accept the certificate.

  8. If your configuration is valid, click Settings. Click File Publish Options, and check Remove additional files at destination. This is important for later steps when you publish new web sites to the same Compute Engine instance.

  9. Click Publish to deploy the sample web application. After publishing completes, Visual Studio opens the application in your default web browser:

    Test application

Congratulations! You now have a working ASP.NET application running on a Compute Engine instance.

Cleaning up

If you're done with the tutorials and want to clean up resources that you've allocated, see Cleaning Up.

Send feedback about...