首页 > 生活

手把手教你写dotnet core(入门篇)

更新时间:2025-05-16 05:21:22 阅读: 评论:0

dotnet core(入门篇)

开发环境准备

dotnet core最低开发环境要求就是一个.NET SDK,在这里可以下载的到最新版本的SDK,各个平台都有.

理论上有了SDK什么事都能做了.

安装SDK的步骤参考上面的连接就OK,这部分我们跳过.

简单讲一下不同操作系统的开发工具选择.

Windows平台下首选Visual Studio 2017,安装的时候选择 .NET Core部分即可,安装下来估计占用磁盘空间5G,同时会帮你装好SDK的,好用,很好用.MacOS/Linux平台选择 SDK + Visual Studio Code + Debug插件 + Nuget插件,很不错,完全生产级别备选方案 Jetbrains家的rider,暂时没用过,resharper一直好评如潮

今天我这边主要是是以VS Code + Debug插件 + 命令行来进来讲解.

装好dotnet core SDK之后,打开命令行界面,输入dotnet看看.

Windows中为CMD或者Powershell,MacOS/Linux为终端Usage: dotnet [options]Usage: dotnet [path-to-application]Options: -h|--help Display help. --version Display version.path-to-application:

再输入dotnet --version查看一下当前版本,我这边显示2.1.4.

输入dotnet help

.NET Command Line Tools (2.1.4)Usage: dotnet [runtime-options] [path-to-application]Usage: dotnet [sdk-options] [command] [arguments] [command-options]path-to-application: The path to an application .dll file to execute.SDK commands: new Initialize .NET projectngagos. restore Restore dependencies specified in the .NET project. run Compiles and immediately executes a .NET project. build Builds a .NET project. publish Publishes a .NET project for deployment (including the runtime). test Runs unit tests using the test runner specified in the project. pack Creates a NuGet package. migratteleperformancee Migrates a project.json based project to a msbuild based project. clean Clean build output(s). sln Modify solution (SLN) files. add Add reference to the project. remove Remove reference from the project. list List reference in the project. nuget Provides additional NuGet commands. msbuild Runs Microsoft Build Engine (MSBuild). vstest Runs Microsoft Test Execution Command Line Tool.Co手机测试mmon options: -v|--verbosity Set the verbosity level of the command. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]. -h|--help Show help.

有类似的这些信息,说明我们的SDK安装以及完成了.

Visual Studio 和Visual Studio Code的安装就不多说了.

创建 dotnet core程序

我这边只有SDK + VS Code环境,创建程序直接使用命令行了.

dotnet core SDK中已经有很多现成的APP模板,我们直接使用dotnet new命令就可以创建对应的程序.

命令行输入 " dotnet new ", 显示如下:

Usage: new [options]Options: -h, --help Displays help for this command. -l, --list Lists templates containing the specified name. If no name is specified, lists all templates. -n, --name The name for the output being created. If no name is specified, the name of the current directory is used. -o, --output Location to place the generated output. -i, --install Installs a source or a template pack. -u, --uninstall Uninstalls a source or a template pack. --type Filters templates based on available types. Predefined values are "project", "item" or "other". --force Forces content to be generated even if it would change existing files. -lang, --language Specifies the language of the template to create.Templates Short Name Language Tags--------------------------------------------------------------------------------------------------------Console Application console [C#], F#, VB Common/ConsoleClass library classlib [C#], F#, VB Common/LibraryUnit Test Project mstest [C#], F#, VB Test/MSTestxUnit Test Project xunit [C#], F#, VB Test/xUnitASP.NET Core Empty web [C#], F# Web/EmptyASP.NET Core Web App (Model-View-Controller) mvc [C#], F# Web/MVCASP.NET Core Web App razor [C#] Web/MVC/Razor PagesASP.NET 肯尼迪学院Core with Angular angular [C#] Web/MVC/SPAASP.NET Core with React.js react [C#] Web/MVC/SPAASP.NET Co山寨币re with React.js and Redux reactredux [C#] Web/MVC/SPAASP.NET Core Web API webapi [C#], F# Web/WebAPIglobal.json file globaljson ConfigNuGe松下gft Config nugetconfig ConfigWeb Config webconfig ConfigSolution File sln SolutionRazor Page page Web/ASP.NETMVC ViewImports viewimports Web/ASP.NETMVC ViewStart viewstart Web/ASP.NETExamples: dotnet new mvc --auth Individual dotnet new viewimports --namespace dotnet new --help

既然是手把手教程,肯定从最原始的Console Application 开始咯,在命令行中输入命令"dotnet new console -n碳钢 FirstApplication",创建一个名为FirstApplica金属疲劳tion的命令行程序

dotnet new console -n FirstApplication;cd FirstApplication;ls FirstApplication;#✗ cd FirstApplication# FirstApplication git:(master) ✗ ls# FirstApplication.csproj Program.cs obj/

我们切换到FirstApplication文件中,可以看到现在已经有三个文件.简单讲解一下:

FirstApplication.csproj .csproj为项目构建文件(C Sharp Project”),对应maven中的pom.xml或者是gradle中的build.gradleProgram.cs 为程序的主入口, 有一个静态的Main方法obj用于存放编译过程中生成的中间临时文件,一般不用管

我们使用VS Code打开这个文件夹看看.

首次在VS Code中打开带有.csproj文件的文件夹,VS Code会提示是否需要安装相关插件,直接选择是即可.

我们这里要用到的插件主要是"C# for Visual Studio Code (powered by OmniSharp)",直接在插件仓库搜C#基本就能看到.

直接看下Program.cs的代码:

一句话输出"Hello World!"...

我们试着运行一下看看.

有两种方式:

直接在对应项目文件夹位置的命令行中执行dotner run;VS Code debug启动dotnet run

"VS Code-查看-集成终端"可以直接调出终端,并且切到当前项目文件路径,执行 dotnet run输出如下:

➜ FirstApplication git:(master) ✗ dotnet runHello World!VS Code debug

VS Code左侧切到debug(一只虫子的图标),点击调试旁边的绿色按钮开始启动.

终端输出:

调试控制台输出:

都说了Debug了,我们简单也做个debug断点调试.

点击代码文件左侧黑色边栏,鼠标左键单击在第8,9行,对应位置出现断点(小红点),如下图:

再次Debug运行程序.

第8行位置出现黄色条纹,程序处于debug默认等待下一步操作.

左侧可查看相关变量当前值,正上方有debug美国精神病人相关操作(F5继续,F10单步跳过,F11单步调试...)

F5按一下,黄色条纹往下走一步到第9行(上一步也下了断点).此时尚未输出任何的信息.

F5下一步执行Console.WriteLine("Hello World!");输出"Hello World!"

写个累加程序试试水

using System;namespace FirstApplication{ class Program { static void Main(string[] args) { var sum = 0; for (var i = 0; i < 10; i++) { sum = sum + i; Console.WriteLine($"当前i值为:{i},sum:{sum}"); } } }}/*Loaded '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.0.5/System.Runtime.Extensions.dll'. Skipped loading symbols. Module is optimized and the de大江论坛bugger option 'Just My Code' 玩咖is enabled.当前i值为:0,sum:0当前i值为:1,sum:1当前i值为:2,sum:3当前i值为:3,sum:6当前i值为:4,sum:10当前i值为:5,sum:15当前i值为:6,sum:21当前i值为:7,sum:28当前i值为:8,sum:36当前i值为:9,sum:45The program '[32086] FirstApplication.dll' has exited with code 0 (0x0).*/

在循环里面打个断点看看i的值和sum的值.

鼠标移动到对应变量上.

到这里,第一个dotnet core程序基本已经完成了,本教程结束....

骗你的,这里还有

还记得我们上面看到的FirstApplication.csproj吗?

我们直接在VS Code中打开看看.(VS用户随便找个文件编辑器打开)

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.0</TargetFramework> </PropertyGroup></P两会照片roject>

这里就指定了SDK版本和程序版本,没有其他多余的东西了.

暂时没什么看的,我们找个web项目的来看看.

<Project Sdk="Microsoft.NET.Sdk.Web">猫会说话; <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> </PropertyGroup> <ItemGroup> <Folder Include="wwwroot\"/> </ItemGroup> 苏菲玛索初吻<ItemGroup> <PackageReference Include="Dapper" Version="1.50.4"/> <PackageReference Include="Microsoft.Appli淘宝福袋cationInsights.AspNetCore" Version="2.2.1"/> <PackageReference Include="Microsoft.AspNetCore" Version="2.0.2"/> <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.3"/> <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.2"/> <惊悚电影推荐PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.2"/> <PackageReference Include="Microsoft.Extensions.Log嗓子痒ging.Debug" Version="2.0.1"/> <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.2"/> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGenera阿尔法男tion.Design" Version="2.0.3"/> <PackageReference Include="Newtonsoft.Json" Version="11.0.2"/> <PackageReference Include="Sta十万青年十万军ckExchange.Redis.StrongName" Version="1.2.6"/> <PackageReference Include="System.Diagnostics.Tools" Version="4.3.0"/> &l陈晓赵丽颖t;PackageReference Include="System.Text.Encoding.CodePages" Version="4.4.0"/> <PackageReference Include="System.ValueTuple" Version="*"/> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0-rc1-final"/> <PackageReference Include="Pomelo.AspNetCore.TimedJob" Version="2.0.0-rtm-10046"/> <PackageReference Include="MySqlConnector" Version="0.40.3"/> <PackageReference Include="MongoDB.Driver.Core" Version="2.7.0-beta0001"/> <PackageReference Include="MongoDB.Driver" Version="2.7.0-beta0001"/> </ItemGroup></Project>

这边能看的东西就很多了.

Project Sdk="Microsoft.NET.Sdk.Web" SDk为WebFolder Include="wwwroot\" 包含 wwwroot静态文件PackageReference Include="Dapper" Version="1.50.4" 引用了Dapper程序包(一个ORM框架)PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.3" 引用了MVC框架PackageReference Include="Newtonsoft.Json" Version="11.0.2" 引用了Newtonsoft.Json Json库

这里我们先看看,具体内容在下一讲asp core 入门我们会详细讲解.

全文完....

本文发布于:2023-06-07 02:49:55,感谢您对本站的认可!

本文链接:http://www.ranqi119.com/ge/85/243313.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 站长QQ:55-9-10-26|友情:优美诗词|电脑我帮您|扬州装修|369文学|学编程|软件玩家|水木编程|编程频道