我的 MacOS/Linux 工具 .NET Core 设置 Docker-ing 你的人生 编辑还是不编辑 Hello World-ing

2025-06-04

我的 MacOS/Linux .NET Core 设置

工具

Docker 化你的生活

是否要编辑

你好,世界

太疯狂了吧?

谁会想到微软会在 Windows 之外让 .NET 开发变得如此出色!

我需要使用这些工具,因为我目前在大学学习的一门课程要求我使用实体框架和一些更花哨的缩写来制作一个 ASP.NET MVC Web 应用程序。

所以,今天我将回顾一下我开始做事所采取的步骤。

在我们开始Hello World左右打印之前,我们必须安装一些工具:

工具

为了验证一切顺利,请继续在终端中执行以下命令:

dotnet --version
2.1.403
Enter fullscreen mode Exit fullscreen mode


docker --version
Docker version 18.06.1-ce, build e68fc7a
Enter fullscreen mode Exit fullscreen mode

Docker 化你的生活

由于我们目前没有适用于 MacOS 的 Microsoft SQL Server 引擎,因此我不得不使用 Docker 在容器中安装 Linux 的 MS SQL Server,尽管这乍一听可能很复杂,但@reverentgeek写了一篇非常棒的文章,可以教你如何做到这一点,甚至更多!

是否要编辑

工具部分我提到过您可以使用VS CodeRider,因此请随意使用您喜欢的任何一个。

如果您使用 VS Code,则必须安装C# 语言扩展,然后就可以开始了。

我个人更喜欢使用 Rider,因为我对基于 IntelliJ 的 IDE 的功能和键盘快捷键比较熟悉。但为了方便起见,我将介绍使用 VS Code 的步骤,因为它是 .NET Core 网站推荐的。

你好,世界

为了启动一个项目,.NET Core SDK 为您提供了一些可以在终端中使用的便捷命令。

要创建项目,请使用以下cd <folder-name>命令导航到您喜欢的文件夹,例如:

cd Documents
Enter fullscreen mode Exit fullscreen mode

然后使用以下mkdir <folder-name>命令创建文件夹:

mkdir CoreIsAwesome
Enter fullscreen mode Exit fullscreen mode

然后使用以下命令导航至该目录:

cd CoreIsAwesome
Enter fullscreen mode Exit fullscreen mode

注意:关于这一点有一个重要的细节,如果您使用下一个命令创建一个 .NET Core 项目,它将被命名为您当前所在的文件夹,在本例中为“CoreIsAwesome”。

现在我们可以用以下方法生成一个示例控制台应用程序:

dotnet new console
Enter fullscreen mode Exit fullscreen mode

您应该很快就会在终端中看到类似这样的内容:

The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on /Users/chrisvasqm/VisualStudioProjects/CoreIsAwesome/CoreIsAwesome.csproj...
  Restoring packages for /Users/chrisvasqm/VisualStudioProjects/CoreIsAwesome/CoreIsAwesome.csproj...
  Generating MSBuild file /Users/chrisvasqm/VisualStudioProjects/CoreIsAwesome/obj/CoreIsAwesome.csproj.nuget.g.props.
  Generating MSBuild file /Users/chrisvasqm/VisualStudioProjects/CoreIsAwesome/obj/CoreIsAwesome.csproj.nuget.g.targets.
  Restore completed in 211.77 ms for /Users/chrisvasqm/VisualStudioProjects/CoreIsAwesome/CoreIsAwesome.csproj.

Restore succeeded.
Enter fullscreen mode Exit fullscreen mode

现在您应该能够在资源管理器面板中看到项目的所有文件。

如果您选择一个文件,假设Program.cs您应该有以下代码:

using System;

namespace CoreIsAwesome
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

大约几秒钟后,VS Code 应该会显示如下对话框:

必需资产对话框

继续并单击“是”。

现在,如果你转到面板Debug,你Run Configuration现在应该是这样的:

运行配置

这意味着我们可以继续运行我们的代码!

按下绿色Play按钮并祈祷不会出现任何错误。

等待几秒钟直到构建完成...

再多一点...

和...

瞧!

控制台上打印的 Hello World

如果您放大或用力挤眼睛,您应该能够在调试控制台中看到那个小小的“Hello World!”。

但...

记住,能力越大,责任越大

就我的具体情况而言,唯一的区别是,不要这样做:

dotnet new console
Enter fullscreen mode Exit fullscreen mode

我用过:

dotnet new mvc
Enter fullscreen mode Exit fullscreen mode

为我生成一个 .NET Core MVC 模板,然后我必须在我的项目中设置 Entity Framework Core 依赖项等等,但这可能是另一篇文章的主题;)

提示:如果您好奇该dotnet new命令可以为您生成哪些其他模板,请使用以下命令:

dotnet new --help
Enter fullscreen mode Exit fullscreen mode

这应该会给你提供你需要的所有说明。

文章来源:https://dev.to/chrisvasqm/my-net-core-setup-for-macoslinux-4cbk
PREV
您对开源的第一个贡献是什么?
NEXT
如何更改所有提交的作者