为 Azure 应用服务配置 ASP.NET 应用Configure an ASP.NET app for Azure App Service

必须将 ASP.NET 应用作为编译的二进制文件部署到 Azure 应用服务。ASP.NET apps must be deployed to Azure App Service as compiled binaries. Visual Studio 发布工具先构建解决方案,然后直接部署已编译的二进制文件,而应用服务部署引擎则先部署代码存储库,然后编译二进制文件。The Visual Studio publishing tool builds the solution and then deploys the compiled binaries directly, whereas the App Service deployment engine deploys the code repository first and then compiles the binaries.

本指南为 ASP.NET 开发者提供了重要概念和说明。This guide provides key concepts and instructions for ASP.NET developers. 如果你从未使用过 Azure 应用服务,则应首先按照 ASP.NET 快速入门将 ASP.NET 与 SQL 数据库配合使用教程进行操作。If you've never used Azure App Service, follow the ASP.NET quickstart and ASP.NET with SQL Database tutorial first.

显示受支持的 .NET Framework 运行时版本Show supported .NET Framework runtime versions

在应用服务中,Windows 实例已安装了所有受支持的 .NET Framework 版本。In App Service, the Windows instances already have all the supported .NET Framework versions installed. 若要显示可供使用的 .NET Framework 运行时和 SDK 版本,请导航到 https://<app-name>.scm.chinacloudsites.cn/DebugConsole 并在基于浏览器的控制台中运行相应的命令:To show the .NET Framework runtime and SDK versions available to you, navigate to https://<app-name>.scm.chinacloudsites.cn/DebugConsole and run the appropriate command in the browser-based console:

对于 CLR 4 运行时版本(.NET Framework 4 及更高版本):For CLR 4 runtime versions (.NET Framework 4 and above):

ls "D:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\"

最新 .NET Framework 版本可能不会立即可用。Latest .NET Framework version may not be immediately available.

对于 CLR 2 运行时版本(.NET Framework 3.5 及更低版本):For CLR 2 runtime versions (.NET Framework 3.5 and below):

ls "D:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\"

显示当前的 .NET Framework 运行时版本Show current .NET Framework runtime version

在 Azure CLI 中运行以下命令:Run the following command in the Azure CLI:

az webapp config show --resource-group <resource-group-name> --name <app-name> --query netFrameworkVersion

值为 v4.0 表示使用的是最新的 CLR 4 版本 (.NET Framework 4.x)。A value of v4.0 means the latest CLR 4 version (.NET Framework 4.x) is used. 值为 v2.0 表示使用的是 CLR 2 版本 (.NET Framework 3.5)。A value of v2.0 means a CLR 2 version (.NET Framework 3.5) is used.

设置 .NET Framework 运行时版本Set .NET Framework runtime version

默认情况下,应用服务使用受支持的最新 .NET Framework 版本来运行 ASP.NET 应用。By default, App Service uses the latest supported .NET Framework version to run your ASP.NET app. 若要改用 .NET Framework 3.5 来运行应用,请在 Azure CLI 中运行以下命令(v2.0 表示 CLR 2):To run your app using .NET Framework 3.5 instead, run the following command in the Azure CLI (v2.0 signifies CLR 2):

az webapp config set --resource-group <resource-group-name> --name <app-name> --net-framework-version v2.0

访问环境变量Access environment variables

在应用服务中,可以在应用代码外部设置应用设置和连接字符串。In App Service, you can set app settings and connection strings outside of your app code. 然后,你可以使用标准 ASP.NET 模式在任何类中访问它们:Then you can access them in any class using the standard ASP.NET pattern:

using System.Configuration;
...
// Get an app setting
ConfigurationManager.AppSettings["MySetting"];
// Get a connection string
ConfigurationManager.ConnectionStrings["MyConnection"];
}

如果你在应用服务中和 web.config 中配置了具有相同名称的应用设置,则应用服务值将优先于 web.config 值。If you configure an app setting with the same name in App Service and in web.config, the App Service value takes precedence over the web.config value. 本地 web.config 值允许你在本地调试应用,但应用服务值允许你使用生产设置在生产中运行应用。The local web.config value lets you debug the app locally, but the App Service value lets your run the app in product with production settings. 连接字符串的使用方式与此相同。Connection strings work in the same way. 这样,你可以将应用程序机密保存在代码存储库外部,无需更改代码便可访问相应的值。This way, you can keep your application secrets outside of your code repository and access the appropriate values without changing your code.

部署多项目解决方案Deploy multi-project solutions

如果 Visual Studio 解决方案包含多个项目,则说明 Visual Studio 发布过程已包括选择要部署的项目的操作。When a Visual Studio solution includes multiple projects, the Visual Studio publish process already includes selecting the project to deploy. 当你部署到应用服务部署引擎(例如,使用 Git 或 ZIP 部署)时,如果启用了“生成自动化”,则应用服务部署引擎会选取它发现的第一个网站或 Web 应用程序项目作为应用服务应用。When you deploy to the App Service deployment engine, such as with Git or with ZIP deploy, with build automation turned on, the App Service deployment engine picks the first Web Site or Web Application Project it finds as the App Service app. 你可以通过指定 PROJECT 应用设置来指定应用服务应当使用哪个项目。You can specify which project App Service should use by specifying the PROJECT app setting. 例如,在 Azure CLI 中运行以下命令:For example, run the following in the Azure CLI:

az webapp config appsettings set --resource-group <resource-group-name> --name <app-name> --settings PROJECT="<project-name>/<project-name>.csproj"

获取详细的异常页面Get detailed exceptions page

当 ASP.NET 应用在 Visual Studio 调试器中生成异常时,浏览器会显示一个详细的异常页面,但在应用服务中,该页面被常规错误消息替换。When your ASP.NET app generates an exception in the Visual Studio debugger, the browser displays a detailed exception page, but in App Service that page is replaced by a generic error message. 若要在应用服务中显示详细的异常页面,请打开 Web.config 文件,在 <system.web> 元素下添加 <customErrors mode="Off"/> 元素。To display the detailed exception page in App Service, open the Web.config file and add the <customErrors mode="Off"/> element under the <system.web> element. 例如:For example:

<system.web>
    <customErrors mode="Off"/>
</system.web>

使用更新后的 Web.config 重新部署你的应用。你现在应会看到相同的详细异常页面。Redeploy your app with the updated Web.config. You should now see the same detailed exception page.

访问诊断日志Access diagnostic logs

你可以使用 System.Diagnostics.Trace 在应用程序代码中添加诊断消息。You can add diagnostic messages in your application code using System.Diagnostics.Trace. 例如:For example:

Trace.TraceError("Record not found!"); // Error trace
Trace.TraceWarning("Possible data loss"); // Warning trace
Trace.TraceInformation("GET /Home/Index"); // Information trace

若要访问应用服务中的应用程序代码内生成的控制台日志,请在 Azure CLI 中运行以下命令以打开诊断日志记录:To access the console logs generated from inside your application code in App Service, turn on diagnostics logging by running the following command in the Azure CLI:

az webapp log config --resource-group <resource-group-name> --name <app-name> --application-logging true --level Verbose

--level 的可能值为:ErrorWarningInfoVerbosePossible values for --level are: Error, Warning, Info, and Verbose. 每个后续级别包括上一个级别。Each subsequent level includes the previous level. 例如:Error 仅包含错误消息,Verbose 则包含所有消息。For example: Error includes only error messages, and Verbose includes all messages.

启用诊断日志记录功能以后,请运行以下命令来查看日志流:Once diagnostic logging is turned on, run the following command to see the log stream:

az webapp log tail --resource-group <resource-group-name> --name <app-name>

如果没有立即看到控制台日志,请在 30 秒后重新查看。If you don't see console logs immediately, check again in 30 seconds.

备注

也可通过浏览器在 https://<app-name>.scm.chinacloudsites.cn/api/logs/docker 中检查日志文件。You can also inspect the log files from the browser at https://<app-name>.scm.chinacloudsites.cn/api/logs/docker.

若要随时停止日志流式处理,请键入 Ctrl+CTo stop log streaming at any time, type Ctrl+C.

后续步骤Next steps