Mac OS X
In addition to Microsoft's own Windows platform,. NET Core provides good support for Mac OS and a variety of Linux (RHEL, Ubuntu, Debian, Fedora, CentOS, SUSE, etc.) Let's take a look at using a Mac to develop a. NET core application, before we routinely build our development environment.
First, install the development environment
Like Windows, if we were to develop a. NET core application on a Mac in the afternoon, we just had to install the. NET Core SDK and the appropriate IDE. For the former, we can directly from the Microsoft Official site (Https://www.microsoft.com/net/core#macos), after the SDK installation we will have. NET core for Mac OS Runtime and corresponding tools, which are included in the. Net Core multiplatform development Experience [1]: Windows frequently used command-line tools dotnet. After the. NET Core SDK Installation is complete, we can execute the dotnet–info command to verify that the SDK was successfully installed. If the SDK is installed successfully, we can see that the runtime environment information shown is matched to the currently installed operating system, as shown in the basic information.
For developers of. NET core Apps for Mac OS X, they also have a wide range of choices on the IDE. First, Visual Studio now has a Mac version, and although there is a gap between functionality and stability with the version of Windows, it is a boon for Mac users accustomed to Visual Studio. In addition, we can choose the free visual Studio Code, if you do not like this style of the IDE, you can also choose JetBrains Rider.
Ii. creating a. NET Core program using the command line
For Windows and Mac users, the development experience for them is basically consistent because the. NET Core SDK provides the command line (primarily the dotnet command-line tool) with a consistent API definition, and they can choose the same IDE (Visual Studio and Visual Studio Code). Let's start with the dotnet new Scaffold command-line tool to create a console app named HelloWorld as shown.
(right) is shown in the project structure of the console app created by the scaffolding command line, and you can see that it has the same file structure as the app we created on Windows to perform the same command. Not only that, the resulting file content is also exactly the same, and the contents of the Helloworld.csproj file and program file Program.cs file that defines the project are as follows.
Helloworld.csproj
<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! ");}}}
We can start the console program directly by executing the dotnet command line without having to make any changes to the created application. As shown in the following code snippet, after switching the current directory to the root of the project where the console app is located, we directly execute the dotnet run command to start the program, and then the "Hello World" string that is output to the console in the main method is printed.
iii. transforming into ASP. NET Core MVC application
In the. NET core multi-platform development experience [1]: Windows, we demonstrated how to transform a console application created from a scaffold command line into an ASP. NET core MVC application, since. So the same way that we've modified the ASP. NET Core MVC app can also run on Mac OS, and we'll verify it later.
We first need to install the required NuGet packages, although the two core types of webhost and webhostbuilder are defined in the NuGet package "Microsoft.AspNetCore.Hosting", but the ASP. The MVC framework provides the implementation of the "MICROSOFT.ASPNETCORE.MVC" dependency on this NuGet package, so we just need to install the latter. In addition, we need to install the NuGet package "Microsoft.AspNetCore.Server.Kestrel" that provides cross-platform server kestrelhttpserver. The installation of several NuGet packages described above also applies here, and we now choose how to install the command line, for which we will execute the dotnet add Package command as shown to install the two NuGet packages on the current project.
Regardless of the way we use it, the NuGet package installed will eventually be reflected in the. cspoj file that defines the project, so open the Helloworld.csproj file and we'll see the following, the two NuGet packages we installed are embodied in <ItemGroup> Two < packagereference> elements under a node.
<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 installation of the required NuGet package, we chose the appropriate IDE or plain text editor to make the following modifications to the Program.cs, then our application became a simple ASP. NET Core MVC application. After the above introduction, I believe that the reader friends have been very clear about the meaning of each line of code, so here we do not explain the introduction.
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 the programming has been done, and we just need to execute the dotnet Run command in a familiar way to start the program. After the program is started, the address "http://localhost:5000/hello/foobar" is accessed using the browser, and we get the output as shown in 1-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
Mac OS X