Get started with .NET Core for c9 users

ubuntu
c-sharp

#1

Get started with .NET Core for c9 users

.NET Core is a cross-platform .NET implementation for websites, servers, and console apps on Windows, Linux, and macOS.

In this tutorial, we are going to install the current version of .NET Core to build and run apps written in C#: Console app and ASP .NET app.

1. Add the dotnet product feed

Register the trusted Microsoft signature key:
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
Register the Microsoft Product feed
# Ubuntu 17.10
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-artful-prod artful main" > /etc/apt/sources.list.d/dotnetdev.list'

# Ubuntu 17.04
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-zesty-prod zesty main" > /etc/apt/sources.list.d/dotnetdev.list'

# Ubuntu Ubuntu 16.04 / Linux Mint 18
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod xenial main" > /etc/apt/sources.list.d/dotnetdev.list'

# Ubuntu 14.04 / Linux Mint 17
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-trusty-prod trusty main" > /etc/apt/sources.list.d/dotnetdev.list'

# run 'lsb_release -a' to check ubuntu version

2. Install .NET SDK

sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-2.1.4

3. Create your app

3.1. Console app

dotnet new console -o myApp
cd myApp

3.1.1. Default code for Program.cs

/// <summary>
/// Program.cs - Default program
/// </summary>
using System;

namespace myApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

3.1.2. Run your app

dotnet run

3.2. ASP .NET Core app

dotnet new razor -o appName
cd appName

3.2.1 Modify Program.cs

///<summary>
/// ./appName/Program.cs
///</summary>
///<remarks>
/// args will have values of $IP and $PORT, so:
/// args[0] -> $IP
// args[1] -> $PORT
///</remarks>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace primerAspNetCoreApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string ipAddress = args[0];
            string port = args[1];
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .UseUrls("http://" + ipAddress + ":" + port + "/")
                .Build();
            
            host.Run();
        }
    }
}

3.2.2. Run the app

dotnet run $IP $PORT

3.2.3. Preview your app.

Go to Preview > Preview Running Application to open a new browser tab and preview your ASP .NET Core application

Congratulations, you have built and run your first .NET apps!

You can get more information at this link.


#2

curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/tru

Ubuntu 17.04

sudo sh -c ‘echo “deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-zesty-prod zesty main” > /etc/apt/sources.list.d/dotnetdev.list’

Ubuntu Ubuntu 16.04 / Linux Mint 18

sudo sh -c ‘echo “deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod xenial main” > /etc/apt/sources.list.d/dotnetdev.list’

Ubuntu 14.04 / Linux Mint 17

sudo sh -c ‘echo “deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-trusty-prod trusty main” > /etc/apt/sources.list.d/dotnetdev.list’

run ‘lsb_release -a’ to check ubuntu version

  1. Install .NET SDK
    sudo apt-get install apt-transport-https
    sudo apt-get update
    sudo apt-get install dotnet-sdk-2.1.4
  2. Create your app
    3.1. Console app
    dotnet new console -o myApp
    cd myApp
    3.1.1. Default code for Program.cs
    ///
    /// Program.cs - Default program
    ///

    using System;

namespace myApp
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(“Hello World!”);
}
}
}
3.1.2. Run your app
dotnet run
3.2. ASP .NET Core app
dotnet new razor -o appName
cd appName
3.2.1 Modify Program.cs
///


/// ./appName/Program.cs
///

///
/// args will have values of $IP and $PORT, so:
/// args[0] -> $IP
// args[1] -> $PORT
///
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace primerAspNetCoreApp
{
public class Program
{
public static void Main(string[] args)
{
string ipAddress = args[0];
string port = args[1];
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup()
.UseUrls(“http://” + ipAddress + “:” + port + “/”)
.Build();

        host.Run();
    }
}

}
3.2.2. Run the app
dotnet run $IP $PORT
3.2.3. Preview your app.
Go to Preview > Preview Running Application to open a new browser tab and preview your ASP .NET Core application

Congratulations, you have built and run your first .NET apps!

You can get more information at this link.

2 Likes
Topic Controls
Reply

Normal
You will be notified if someone mentions your @name or replies to you.

Suggested Topics
Running pouchdb-server on cloud9
1
3h
Programming Help
Lost one day of work
1
7h
Bugs
Using the C debugger
1
9h
Cloud9 Support
@showFoldWidgets value and behaviour not persistent between sessions
1
10h
Bugs
Request Host Not Whitelisted When Running Flask
0
23h
Cloud9 Support
There are 9 new topics remaining, or browse other topics in
How To
Quot

This text will be blurred