通过在 Visual Studio 中导入发布设置将应用程序发布到 IISPublish an application to IIS by importing publish settings in Visual Studio

可使用“发布”工具导入发布设置,然后部署应用。You can use the Publish tool to import publish settings and then deploy your app. 在本文中,我们使用 IIS 的发布设置,但你可以使用类似的步骤导入 Azure 应用服务的发布设置。In this article, we use publish settings for IIS, but you can use similar steps to import publish settings for Azure App Service. 在某些情况下,对于每个 Visual Studio 安装,使用发布设置配置文件比为 IIS 手动配置部署要快。In some scenarios, use of a publish settings profile can be faster than manually configuring deployment to IIS for each installation of Visual Studio.

这些步骤适用于 Visual Studio 中的 ASP.NET、ASP.NET Core 和 .NET Core 应用。These steps apply to ASP.NET, ASP.NET Core, and .NET Core apps in Visual Studio.

在本教程中,你将:In this tutorial, you will:

  • 配置 IIS 以生成发布设置文件Configure IIS so that you can generate a publish settings file
  • 创建发布设置文件Create a publish settings file
  • 将发布设置文件导入 Visual StudioImport the publish settings file into Visual Studio
  • 将应用部署到 IISDeploy the app to IIS

发布设置文件 (*.publishsettings) 与在 Visual Studio 中创建的发布配置文件 (*.pubxml) 不同 。A publish settings file (*.publishsettings) is different than a publishing profile (*.pubxml) created in Visual Studio. 发布设置文件由 IIS 或 Azure 应用服务创建,或者可手动创建,然后可将其导入 Visual Studio。A publish settings file is created by IIS or Azure App Service, or it can be manually created, and then it can be imported into Visual Studio.

备注

若只需要将 Visual Studio 发布配置文件(*.pubxml 文件)从一个 Visual Studio 安装复制到另一个,则对于托管项目类型可以在 \<projectname>\Properties\PublishProfiles 文件夹中查找发布配置文件 <profilename>.pubxml 。If you just need to copy a Visual Studio publishing profile (*.pubxml file) from one installation of Visual Studio to another, you can find the publishing profile, <profilename>.pubxml, in the \<projectname>\Properties\PublishProfiles folder for managed project types. 对于网站则在 \App_Data 文件夹下进行查找。For websites, look under the \App_Data folder. 发布配置文件是 MSBuild XML 文件。The publishing profiles are MSBuild XML files.

先决条件Prerequisites

  • 必须安装 Visual Studio 2019 且具有 ASP.NET 和 web 开发工作负载。You must have Visual Studio 2019 installed and the ASP.NET and web development workload.

    如果尚未安装 Visual Studio,请转到 Visual Studio 下载页免费安装。If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.

  • 须安装 Visual Studio 2017 且具有 ASP.NET 和 web 开发工作负载。You must have Visual Studio 2017 installed and the ASP.NET and web development workload.

    如果尚未安装 Visual Studio,请转到 Visual Studio 下载页免费安装。If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.

  • 服务器上必须运行 Windows Server 2012、Windows Server 2016 或 Windows Server 2019,并且必须正确安装 IIS Web 服务器角色(需要生成发布设置文件 (*.publishsettings))。On your server, you must be running Windows Server 2012, Windows Server 2016, or Windows Server 2019, and you must have the IIS Web Server role correctly installed (required to generate the publish settings file (*.publishsettings)). 该服务器上还必须安装 ASP.NET 4.5 或 ASP.NET Core。Either ASP.NET 4.5 or ASP.NET Core must also be installed on the server. 若要安装 ASP.NET 4.5,请参阅使用 ASP.NET 3.5 和 ASP.NET 4.5 的 IIS 8.0To set up ASP.NET 4.5, see IIS 8.0 Using ASP.NET 3.5 and ASP.NET 4.5. 若要安装 ASP.NET Core,请参阅 使用 IIS 在 Windows 上托管 ASP.NET CoreTo set up ASP.NET Core, see Host ASP.NET Core on Windows with IIS. 对于 ASP.NET Core,请确保将“应用程序池”配置为使用“无托管代码”,如文章中所述。For ASP.NET Core, make sure you configure the Application Pool to use No Managed Code, as described in the article.

在 Visual Studio 中新建 ASP.NET 项目Create a new ASP.NET project in Visual Studio

  1. 在运行 Visual Studio 的计算机上,创建新项目。On the computer running Visual Studio, create a new project.

    选择正确的模板。Choose the correct template. 在此示例中,选择“ASP.NET Web 应用程序(.NET Framework)”或“ASP.NET Core Web 应用程序”(仅限 C#),然后选择“确定” 。In this example, choose either ASP.NET Web Application (.NET Framework) or (for C# only) ASP.NET Core Web Application, and then select OK.

    如果没有看到指定的项目模板,请转到“新建项目”对话框左侧窗格中的“打开 Visual Studio 安装程序”链接 。If you don't see the specified project templates, go to the Open Visual Studio Installer link in the left pane of the New Project dialog box. Visual Studio 安装程序启动。The Visual Studio Installer launches. 安装“ASP.NET 和 Web 开发”工作负载。Install the ASP.NET and web development workload.

    选择的项目模板(ASP.NET 或 ASP.NET Core)必须与安装在 Web 服务器上的 ASP.NET 版本相对应。The project template you choose (ASP.NET or ASP.NET Core) must correspond to the version of ASP.NET installed on the web server.

  2. 选择“MVC”(.NET Framework) 或“Web 应用程序(模型-视图-控制器)”(适用于 .NET Core),并确保已选中“无身份验证”,然后选择“确定” 。Choose either MVC (.NET Framework) or Web Application (Model-View-Controller) (for .NET Core), and make sure that No Authentication is selected, and then select OK.

  3. 键入名称(例如“MyWebApp”),然后选择“确定” 。Type a name like MyWebApp and select OK.

    Visual Studio 随即创建项目。Visual Studio creates the project.

  4. 选择“生成” > “生成解决方案”以生成项目 。Choose Build > Build Solution to build the project.

在 Windows Server 上安装和配置 Web 部署Install and configure Web Deploy on Windows Server

用于托管服务器的 Web 部署 3.6 提供额外的配置功能,可实现从 UI 创建发布设置文件。Web Deploy 3.6 for Hosting Servers provides additional configuration features that enable the creation of the publish settings file from the UI.

  1. 如果已在 Windows Server 上安装 Web 部署,请使用“控制面板” > “程序” > “卸载程序”将其卸载。If you have Web Deploy already installed on Windows Server, uninstall it using Control Panel > Programs > Uninstall a Program.

  2. 接下来,在 Windows 服务器上安装用于托管服务器的 Web 部署 3.6。Next, install Web Deploy 3.6 for Hosting Servers on Windows Server.

    要安装用于托管服务器的 Web 部署,请使用 Web 平台安装程序 (WebPI)。To install Web Deploy for Hosting Servers, use the Web Platform Installer (WebPI). (要从 IIS 查找 Web 平台安装程序链接,请选择服务器管理器左侧窗格中的“IIS”。(To find the Web Platform Installer link from IIS, select IIS in the left pane of Server Manager. 在服务器窗格中,右键单击服务器并选择“Internet Information Services (IIS)管理器”。In the server pane, right-click the server and select Internet Information Services (IIS) Manager. 然后,在“操作”窗口中,使用“获取新的 Web 平台组件”连接 。)你还可以从下载获取 Web 平台安装程序 (WebPI)。Then use the Get New Web Platform Components link in the Actions window.) You can also obtain the Web Platform Installer (WebPI) from downloads.

    在 Web 平台安装程序中,在“应用程序”选项卡中查找“用于托管服务器的 Web 部署 3.6”。In the Web Platform Installer, you find Web Deploy 3.6 for Hosting Servers in the Applications tab.

  3. 如果尚未安装“IIS 管理脚本和工具”,请立即安装。If you did not already install IIS Management Scripts and Tools, install it now.

    转到“选择服务器角色” > “Web 服务器(IIS)” > “管理工具”,然后选择“IIS 管理脚本和工具”角色,点击“下一步”,然后安装角色 。Go to Select server roles > Web Server (IIS) > Management Tools, and then select the IIS Management Scripts and Tools role, click Next, and then install the role.

    安装 IIS 管理脚本和工具

    需要脚本和工具来生成发布设置文件。The scripts and tools are required to enable the generation of the publish settings file.

  4. 根据需要,打开“控制面板”>“系统和安全”>“管理工具”>“服务”来验证 Web 部署是否正确运行,然后确保:(Optional) Verify that Web Deploy is running correctly by opening Control Panel > System and Security > Administrative Tools > Services, and then make sure that:

    • “Web 部署代理服务”正在运行(旧版本中的服务名称不同)。Web Deployment Agent Service is running (the service name is different in older versions).

    • “Web 管理服务”正在运行。Web Management Service is running.

    如果某个代理服务未运行,请重新启动“Web 部署代理服务”。If one of the agent services is not running, restart the Web Deployment Agent Service.

    如果 Web 部署代理服务不存在,请转到“控制面板”>“程序”>“卸载程序”,查找“Microsoft Web 部署 <version>。If the Web Deployment Agent Service is not present at all, go to Control Panel > Programs > Uninstall a program, find Microsoft Web Deploy <version>. 选择“更改”安装,并确保对 Web 部署组件选择“将安装到本地驱动器” 。Choose to Change the installation and make sure that you choose Will be installed to the local hard drive for the Web Deploy components. 完成更改安装步骤。Complete the change installation steps.

在 Windows Server 上的 IIS 中创建发布设置文件Create the publish settings file in IIS on Windows Server

  1. 关闭并重新打开 IIS 管理控制台以在 UI 中显示更新的配置选项。Close and reopen the IIS Management Console to show updated configuration options in the UI.

  2. 在 IIS 中,右键单击“默认网站”,选择“部署” > “配置 Web 部署发布” 。In IIS, right-click the Default Web Site, choose Deploy > Configure Web Deploy Publishing.

    配置 Web 部署配置

    如果看不到“部署”菜单,请参阅前面的部分来验证 Web 部署是否正在运行。If you don't see the Deploy menu, see the preceding section to verify that Web Deploy is running.

  3. 在“配置 Web 部署发布”对话框中,检查设置。In the Configure Web Deploy Publishing dialog box, examine the settings.

  4. 单击“设置”。Click Setup.

    在“结果”面板中,输出显示已为指定用户授予访问权限,并且已在对话框中显示的位置生成了具有 .publishsettings 文件扩展名的文件。In the Results panel, the output shows that access rights are granted to the specified user, and that a file with a .publishsettings file extension has been generated in the location shown in the dialog box.

    <?xml version="1.0" encoding="utf-8"?>
    <publishData>
      <publishProfile
        publishUrl="https://myhostname:8172/msdeploy.axd"
        msdeploySite="Default Web Site"
        destinationAppUrl="http://myhostname:80/"
        mySQLDBConnectionString=""
        SQLServerDBConnectionString=""
        profileName="Default Settings"
        publishMethod="MSDeploy"
        userName="myhostname\myusername" />
    </publishData>
    

    根据 Windows Server 和 IIS 配置,可以在 XML 文件看到不同的值。Depending on your Windows Server and IIS configuration, you see different values in the XML file. 下面是有关所看到的值的一些详细信息:Here are a few details about the values that you see:

    • publishUrl 属性中引用的 msdeploy.axd 文件是 Web 部署动态生成的 HTTP 处理程序文件。The msdeploy.axd file referenced in the publishUrl attribute is a dynamically generated HTTP handler file for Web Deploy. (用于测试目的,http://myhostname:8172 通常也适用。)(For testing purposes, http://myhostname:8172 generally works as well.)

    • publishUrl 端口设置为端口 8172,这是 Web 部署的默认端口。The publishUrl port is set to port 8172, which is the default for Web Deploy.

    • destinationAppUrl 端口设置为端口 80,这是 IIS 部署的默认端口。The destinationAppUrl port is set to port 80, which is the default for IIS.

    • 如果使用主机名无法在 Visual Studio 中连接到远程主机(在后续步骤中),请测试 IP 地址以代替主机名。If you are unable to connect to the remote host in Visual Studio using the host name (in later steps), test the IP address in place of the host name.

      备注

      如果要发布到在 Azure VM 上运行的 IIS,则必须在网络安全组中打开 Web 部署和 IIS 端口。If you are publishing to IIS running on an Azure VM, you must open the Web Deploy and IIS ports in the Network Security group. 有关详细信息,请参阅安装和运行 IISFor detailed information, see Install and run IIS.

  5. 将此文件复制到运行 Visual Studio 的计算机上。Copy this file to the computer where you are running Visual Studio.

在 Visual Studio 中导入发布设置并进行部署Import the publish settings in Visual Studio and deploy

  1. 如果计算机上的 Visual Studio 中打开有 ASP.NET 项目,则在该计算机的解决方案资源管理器中右键单击该项目,然后选择“发布”。On the computer where you have the ASP.NET project open in Visual Studio, right-click the project in Solution Explorer, and choose Publish.

    如果先前配置了任何发布配置文件,则“发布”窗格会显示。If you have previously configured any publishing profiles, the Publish pane appears. 单击“新建”或“创建新配置文件”。Click New or Create new profile.

  2. 选择该选项以导入配置文件。Select the option to import a profile.

    在“发布”对话框中,单击“导入配置文件”。In the Publish dialog box, click Import Profile.

    在“选取发布目标”对话框中,点击“导入配置文件” 。In the Pick a publish target dialog box, click Import Profile.

    选择发布

  3. 导航到上一节中创建的发布设置文件的位置。Navigate to the location of the publish settings file that you created in the previous section.

  4. 在“导入发布设置文件”对话框中,导航到在上一部分创建的配置文件并选择该文件,然后单击“打开” 。In the Import Publish Settings File dialog, navigate to and select the profile that you created in the previous section, and click Open.

    单击“完成”保存发布配置文件,然后单击“发布”。Click Finish to save the publishing profile, and then click Publish.

    Visual Studio 开始执行部署过程,并且输出窗口将显示进度和结果。Visual Studio begins the deployment process, and the Output window shows progress and results.

    如果出现任何部署错误,请单击“编辑”以编辑设置。If you get an any deployment errors, click Edit to edit settings. 修改设置,然后单击“验证”以测试新设置。Modify settings and click Validate to test new settings. 如果找不到主机名,请尝试“服务器”和“目标 URL”字段中的 IP 地址而不是主机名 。If the host name is not found, try the IP address instead of the host name in the Server and Destination URL fields.

    Visual Studio 开始执行部署过程,并且输出窗口将显示进度和结果。Visual Studio begins the deployment process, and the Output window shows progress and results.

    如果出现任何部署错误,请单击“设置”以编辑设置。If you get an any deployment errors, click Settings to edit settings. 修改设置,然后单击“验证”以测试新设置。Modify settings and click Validate to test new settings. 如果找不到主机名,请尝试“服务器”和“目标 URL”字段中的 IP 地址而不是主机名 。If the host name is not found, try the IP address instead of the host name in the Server and Destination URL fields.

    编辑发布工具中的设置

应用成功部署后,它应自动启动。After the app deploys successfully, it should start automatically. 若在 Visual Studio 中无法启动该应用,请在 IIS 中启动。If it does not start from Visual Studio, start the app in IIS. 对于 ASP.NET Core,需要确保将 DefaultAppPool 的应用程序池字段设置为“无托管代码” 。For ASP.NET Core, you need to make sure that the Application pool field for the DefaultAppPool is set to No Managed Code.

后续步骤Next steps

在本教程中,创建了发布设置文件,将其导入 Visual Studio,并将 ASP.NET 应用部署到 IIS。In this tutorial, you created a publish settings file, imported it into Visual Studio, and deployed an ASP.NET app to IIS. 建议了解 Visual Studio 中其他发布选项的概述。You may want an overview of other publishing options in Visual Studio.