. NET Core multi-platform development experience [2]: Mac OS X, coreos
In addition to Microsoft's own Windows platform ,. NET Core provides good support for Mac OS and various Linux systems (such as RHEL, Ubuntu, Debian, Fedora, CentOS, and SUSE). Let's first try Mac for development.. NET Core application. Before that, we need to build our development environment as an example.
1. Install the development environment
Like Windows, if we develop. NET Core applications on Mac in the afternoon, we only need to install. NET Core SDK and the corresponding IDE. For the former, we can directly from the Microsoft official site (https://www.microsoft.com/net/core#macos), after SDK installation we will have. NET Core for Mac OS Runtime and corresponding tools, including in 《.. NET Core multi-platform development experience [1]: dotnet, a frequently used command line tool in Windows .. After the NET Core SDK is installed, run the dotnet-info command to check whether the SDK is successfully installed. If the SDK is successfully installed, we can see the basic information shown in. The displayed runtime environment information matches the currently installed operating system.
<Project Sdk = "Microsoft. NET. Sdk"> <PropertyGroup> <OutputType> Exe </OutputType> <TargetFramework> netcoreapp2.0 </TargetFramework> </PropertyGroup> </Project>
Pprogram. cs
using System; namespace helloworld{ class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); } }}
You can directly execute the dotnet command line to start the console without making any changes to the created application. As shown in the following code snippet, after switching the current directory to the root directory of the project where the console application is located, run the dotnet run Command to start the program, then the "Hello World" string output to the console in the Main method is printed out.
<Project Sdk = "Microsoft. NET. sdk "> <PropertyGroup> <OutputType> Exe </OutputType> <TargetFramework> netcoreapp2.0 </TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include =" Microsoft. aspNetCore. mvc "Version =" 2.0.0 "/> <PackageReference Include =" Microsoft. aspNetCore. server. kestrel "Version =" 2.0.0 "/> </ItemGroup> </Project>
After the required NuGet package is installed, select the corresponding IDE or plain text editor for the Program. cs makes the following changes, so our application becomes a simple ASP.. NET Core MVC application. After the above introduction, I believe that readers have already understood the meaning of each line of code, so we will not go into detail here.
using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.DependencyInjection;namespace helloworld{ class Program { static void Main(string[] args) { new WebHostBuilder() .UseKestrel() .ConfigureServices(svcs => svcs.AddMvc()) .Configure(app => app.UseMvc()) .Build() .Run(); } } public class HelloController { [HttpGet("/hello/{name}")] public string SayHello(string name) { return $"Hello, {name}"; } }}
So far, all programming work has been completed. We only need to execute the dotnet run Command as we are familiar with to start this program. After the program is started, use the browser to access the address "http: // localhost: 5000/hello/foobar". We will get the output from 1 to 21.
. NET Core multi-platform development experience
. NET Core multi-platform development experience [1]: Windows
. NET Core multi-platform development experience [2]: Mac OS X
. NET Core multi-platform development experience [3]: Linux
. NET Core multi-platform development experience [4]: Docker