ASP.NET Core MVC(五)发布部署

这篇文章主要介绍在Linux服务器上部署ASP.NET Core程序,然后在其他计算机上访问。

一、Linux服务器Runtime环境

进入官方教程https://docs.microsoft.com/zh-cn/dotnet/core/install/linux-ubuntu安装。

二、发布

发布的话选择本地文件夹发布是最容易的,然后通过xftp等工具将本地程序上传至服务器。但是有一点非常重要:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>()
                .UseUrls("http://*:8901"); 
            });
}

添加了这么一句:

.UseUrls("http://*:8901")

*表示任意的IP地址,待会儿我们访问的话就是Linux服务器的主机号;:8901表示端口号

三、运行程序

在服务器上使用命令行运行程序,例如:

zhudk@vm1:/$ dotnet  /expand/zhudk/mvc/mvc.dll
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[62]
      User profile is available. Using '/home/zhudk/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://[::]:8901
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /

四、在本地访问

ASP.NET Core MVC(五)发布部署_第1张图片

但是有一个疑问,界面布局好像在windows上运行的不一致?

五、开机自启

最后,我们可以在服务器上设置我们的程序为开机自启,这样我们就不需要手动管理了。

六、Docker部署

现在流行的应答是Docker部署,但相对来说要复杂一些,暂时不做介绍。

你可能感兴趣的:(Asp.Net,Core,ASP.NET,Core,mvc)