参考地址:https://docs.asp.net/en/latest/tutorials/your-first-mac-aspnet.html
一、使用VSCode创建一个目录FirstWebApp,File->Open Folder->Create Folder
二、初始一个基本的.NET项目
在VSCode中,Ctrl+Shift+P中没有找到这个命令,暂时没搞定,为啥?
命令:dotnet new
三、添加引用包
project.json中添加相关引用
VSCode中Ctrl+Shift+P 输入dotnet restore 单击加载恢复当前项目依赖包
四、添加Startup.cs文件
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions; namespace FirstWebApp { public class Startup { public void Configure(IApplicationBuilder app) { app.Run(async(context)=> { await context.Response.WriteAsync("Hello World!"); }); } } }
Program.cs
using System; using System.IO; using Microsoft.AspNetCore.Hosting; namespace FirstWebApp { public class Program { public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup<Startup>() .Build(); host.Run(); } } }
五、最终结果
VSCode中F5执行发现报错
Unhandled Exception: System.AggregateException: One or more errors occurred. (Error -98 EADDRINUSE address already in use) ---> Microsoft.AspNetCore.Server.Kestrel.Networking.UvException: Error -98 EADDRINUSE address already in use
如果发现这个错误,暂时不知道有啥解决方案,临时对策就是在CentOS 系统管理工具中,找到System Monitor 在Processes 干掉dotnet的全部进程。
命令跑如下:
[sonny@bogon FirstWebApp]$ dotnet run Project FirstWebApp (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation. Hosting environment: Production Content root path: /home/sonny/DotnetProject/FirstWebApp Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down.
六、结果
浏览器中访问localhost:5000 就能看到Hello World!