[ASP.NET Core 3 framework Secret] cross-platform development experience: Linux

If you want to experience the development of .NET Core applications under Linux environment, we have a variety of options. One is installed on a single physical machine native Linux, we can choose a Linux Distribution based on their preferences, now, like RHEL, Ubuntu, Debian, Fedora, CentOS, and SUSE these mainstream Distribution are supported. If readers who think this way too much trouble, we can also take the form of a virtual machine to install the appropriate Linux Distribution, for example, it is Ubuntu on VirtualBox installation I use regularly. For users of the X64 Windows 10, we have a third more convenient option is to use Windows 10 provides Linux subsystem (WSL: Windows Subsystem for Linux), the latest version WSL 2.

First, enable Linux subsystem

WSL 2 requires the operating system must be "Windows 10 build 18917" or higher version. Only need to enable WSL 2 "at 10 on Windows Virtual Machine Platform " and " Windows the Subsystem for Linux " These two properties to open it. We are two ways to enable these two features, the first implementation of Enable-WindowsOptionalFeature command (which needs to be in such a way in PowerShell administrator to perform identity).

Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

We can also employ visual way to turn these two characteristics. Specifically, we can "control panel (Control Panel)"> "Programs and Features (Programs and Features)" opened as shown in "Programs and Features" dialog box, and select the "Turn Windows features on or off) "open" Windows Features "dialog box. After checking the "Virtual Machine Platform" and "Windows Subsytem for Linux" option and identify and restart the machine, the Windows subsystem for Linux was enabled.

1-17

Second, install Linux

After enabling WSL, we can install the corresponding Linux Distribution needed. The most convenient way is to directly download and install the corresponding Linux Distribution in the Microsoft Store, such as Microsoft Store offers a free version as shown below for our Ubuntu 18.04 LTS.

1-18

If Microsoft Store is not available (such as my operating system is not Microsoft Store LTSC version of Windows 10 Enterprise), we can directly use curl.exe or the Invoke-WebRequest to download Linux command line installation package. Listed below are several common Linux Distribution download address.

  • Ubuntu 18.04: https://aka.ms/wsl-ubuntu-1804
  • Ubuntu 18.04 ARM: https://aka.ms/wsl-ubuntu-1804-arm
  • Ubuntu 16.04: https://aka.ms/wsl-ubuntu-1604
  • Debian GNU/Linux: https://aka.ms/wsl-debian-gnulinux
  • Kali Linux: https://aka.ms/wsl-kali-linux
  • OpenSUSE: https://aka.ms/wsl-opensuse-42
  • SLES: https://aka.ms/wsl-sles-12

当Linux被成功安装之后,我们可以在CMD命令行中执行bash命令进入Linux Bash Shell。如下图所示,我们在CMD命令行中执行bash命令之后会自动进入Linux Bash Shell。我们在Bash Shell中执行“lsb_release -a”命令输出当前安装的Linux Distribution的版本信息。如果想恢复到CMD命令行模式,我们只需要执行exit命令退出Bash Shell即可。

1-19

作为主系统的Windows和Linux子系统可以共享网络系统和文件系统。Windows下的文件系统直接挂载到“/mnt”目录下,所以Windows下的文件或者目录路径加上“/mnt”前缀就变成了基于Linux子系统下的路径。如上图所示,CMD命令行环境下的当前工作目录为“c:\Users\jinnan”,当我们切换到Bash Shell后,当前工作目录其实并没有发生变化,只是路径变成了“/mnt/c/Users/jinnan”而已。

三、安装开发环境

如果我们想在Linux下运行.NET Core应用,我们也只需要安装.NET Core SDK就可以了。针对不同的Linux Distribution,.NET Core SDK的安装略有不同。对于主流的Linux发行版本(RHEL、Ubuntu、Debian、Fedora、CentOS和SUSE等), .NET Core官方站点上给出了安装.NET Core SDK的详细教程。对于我们安装的Ubuntu 18.04 LTS,.NET Core 3.0 SDK可以执行如下的命令进行安装。

~$ sudo apt-get update
~$ sudo apt-get install apt-transport-https
~$ sudo apt-get install dotnet-sdk-3.0

安装完成之后,我们可以执行dotnet --info命令查看当前.NET Core SDK的基本信息,如果得到如下图所示的输出结果,那就证明.NET Core SDK 3.0被成功安装。

1-20

四、创建一个ASP.NET Core应用

.NET Core SDK同样为Linux提供了dotnet这个重要的命令行工具,这意味着我们同样可以利用它提供的脚手架模板创建相应的.NET Core应用。这次我们直接创建一个空的ASP.NET Core 应用,为此我们在执行dotnet new命令的时候将模板名称指定为“web”。如下图所示,我们在执行dotnet new命令时的当前工作目录为“/mnt/c/helloworld”,也就是主系统Windows下的“c:\helloworld”,我们列出了该目录下的所有文件和子目录。

1-21

Scaffolding and applications created with command in Windows and Mac OS X, like we in Linux can also be made directly without the need for them to run without any changes to them, and we just need to execute in the directory where the current application dotnet run command. As shown below, after the start ASP .NET Core application by default bindings in 5000 (HTTP) and 5001 (HTTPS) port request listening, because Linux is currently the Windows subsystem rather than a virtual machine, so the master system can be shared network, so we use the browser request address "http: // localhost: 5000" can access the application's home page.

1-22

[ASP.NET Core 3 framework Secret] cross-platform development experience: Windows [Part]
[ASP.NET Core 3 framework Secret] cross-platform development experience: Windows [novellas]
[ASP.NET Core 3 framework Secret] cross-platform development experience: Windows [next]
[3 Core ASP.NET framework Secret] cross-platform development experience: Mac OS
[3 Core ASP.NET framework Secret] cross-platform development experience: Linux
[3 Core ASP.NET framework Secret] cross-platform development experience: Docker

Guess you like

Origin www.cnblogs.com/artech/p/inside-asp-net-core-01-05.html
Recommended