These instructions are for .NET Core v1.0.4 in Linux Mint 18. Instructions for other versions and other distros are very similar.
Please visit https://www.microsoft.com/net/core for more information.
Add the apt-get feed for Linux Mint 18:
sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list' sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 417A0893 sudo apt-get update
Install the .NET Core SDK:
sudo apt-get install dotnet-dev-1.0.4
If you'd like to install a different version, ensure that you're installing the latest version, or just check to see what's available, you can search with apt-cache:
apt-cache search dotnet
Create a test app:
dotnet new console -o testinstallation cd testinstallation
Restore the app's required packages, and run it:
dotnet restore dotnet run
When you see “Hello, World!” as your output, you've verified your installation, and .NET Core is ready to go!
There's no “Visual Studio IDE Community Edition” for Linux yet, but Microsoft has created a very capable cross-platform programmer's editor called Visual Studio Code. It includes many of the handy features and behaviors you've grown used to in the Visual Studio IDE.
After the installation completes, locate Visual Studio Code in your menu and launch it.
Before you start working on any projects, you'll need to install the C# extension:
Visual Studio Code has a very active extension community. I recommend searching the extension marketplace. You'll find lots of useful items.
One example: You'll probably want to install the “C# Extensions” extension, by jchannon. It simplifies the creation of C# classes and interfaces.
Now you're ready to create some projects!
The .NET Core CLI provides several project templates. This command:
dotnet new
…will show you a list, something like this:
Templates Short Name Language Tags ---------------------------------------------------------------------- Console Application console [C#], F# Common/Console Class library classlib [C#], F# Common/Library Unit Test Project mstest [C#], F# Test/MSTest xUnit Test Project xunit [C#], F# Test/xUnit ASP.NET Core Empty web [C#] Web/Empty ASP.NET Core Web App mvc [C#], F# Web/MVC ASP.NET Core Web API webapi [C#] Web/WebAPI Solution File sln Solution
The calling convention to create a project is as follows:
dotnet new <template short name> -o <project name>
The -o argument is optional, but I recommend using it, since it also controls the naming of the files in the project folder, along with creating the folder itself.
Let's step through the creation of the console, mvc, and webapi project types.
A console project is the simplest, and is nice for finding your way around. We'll create a console project called “Hello”, and restore its required packages:
dotnet new console -o Hello cd Hello dotnet restore
At this point, you have a runnable project. Press [F5]. The project will build and run, and you'll see “Hello World!” in your Debug console.
Now you can make any changes you require. As a simple exercise, we'll create a Greeter class to handle our “Hello World” duties.
using System; namespace Hello { public class Greeter { public void SayHello() { Console.WriteLine("Hello, world!"); } } }
Greeter myGreeter = new Greeter(); myGreeter.SayHello();
Press [F5], and you should see “Hello, world!” in the Debug console.
You've now generated a console app, and enhanced it with a simple class!
Now we'll move from a text-based console client to the web.
Generate a MVC web application project:
dotnet new mvc -o SimpleMVC cd SimpleMVC dotnet restore
The project should now be runnable. Press [F5], and the project will build and run. It should launch in your default web browser as localhost on port 5000. You will see a simple application with “Home”, “About”, and “Contact” pages. After you've clicked each page and verified that they load properly, close the browser tab and stop the running project by clicking the “Stop” button in Visual Studio Code.
Let's add a new page called “Company”:
Add the following HTML to Company.cshtml:
@{ ViewData["Title"] = "Company"; } <h2>Company Name</h2> <p>Company info will go here</p>
Open HomeController.cs and add an action result for the new page:
public IActionResult Company() { return View(); }
Open _Layout.cshtml (in Views/Shared) and add a menu entry for the new page in the “nav navbar-nav” section:
<li><a asp-area="" asp-controller="Home" asp-action="Company">Company</a></li>
Press [F5] to compile and run. You should see a new entry for “Company” in the menu. Clicking it should load the new page.
Before moving on to creating a web service, you'll need a way to test your service. I recommend Postman. You can download it here.
For local testing of services, it's as simple as this:
Postman also supports various authentication types, sending complex requests, additional headers, etc, but we won't need these features for the service we're creating.
For our final project, we'll create a simple RESTful web service.
Generate a Web API project:
dotnet new webapi -o SimpleService cd SimpleService dotnet restore
Open the ValuesController.cs file (in the Controllers folder) and you'll see that the template has already generated some endpoints for you. Let's test the api/values endpoint:
You should see this returned in Postman:
[ "value1", "value2" ]
If you look at the matching code in ValuesController.cs:
// GET api/values [HttpGet] public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; }
…you can see that this is what it's supposed to be returning.
Visual Studio Code also has integrated debugging.
Now, stop the service by clicking the “Stop” button in Visual Studio Code, and we'll add our own controller. We'll add a very simple “Get” endpoint that takes a number and returns the square root of that number.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; namespace SimpleService.Controllers { [Route("api/[controller]")] public class MathController : Controller { // GET api/math/5 [HttpGet("{id}")] public string Get(double id) { return Math.Sqrt(id).ToString(); } } }
You should see a “5” returned.
Now, let's take it one step further, and add a Model to do our math work for us.
using System; namespace SimpleService.Models { public class MathProvider { public double GetSquareRoot(double inputValue) { return Math.Sqrt(inputValue); } } }
using SimpleService.Models;
Change the Get method as follows:
public string Get(double id) { MathProvider myMath = new MathProvider(); return myMath.GetSquareRoot(id).ToString(); }
You will see a “5” returned again, but now it will be returned from our new model. (If you'd like, set a breakpoint in the GetSquareRoot method of the MathProvider class to confirm that it's being hit.)
Congratulations, you've created a Web API service and added your own endpoint, along with a new model!
To add nuget packages to an existing project, open a terminal in the root folder of the project, then issue a dotnet command in the following format:
dotnet add <PROJECT> package [arguments] [options]
For example, to add the Microsoft.AspNetCore.StaticFiles package, you'd do this:
dotnet add package Microsoft.AspNetCore.StaticFiles
If you want to add a specific version of a package, use the -v argument, like this:
dotnet add package Microsoft.AspNetCore.StaticFiles -v 1.0.1