針對 ASP.NET Core 專案進行疑難排解和調試Troubleshoot and debug ASP.NET Core projects

作者:Rick AndersonBy Rick Anderson

下列連結提供疑難排解指引:The following links provide troubleshooting guidance:

.NET Core SDK 警告.NET Core SDK warnings

已安裝 .NET Core SDK 的32位和64位版本Both the 32-bit and 64-bit versions of the .NET Core SDK are installed

在 ASP.NET Core 的 [ 新增專案 ] 對話方塊中,您可能會看到下列警告:In the New Project dialog for ASP.NET Core, you may see the following warning:

.NET Core SDK 的32位和64位版本皆已安裝。Both 32-bit and 64-bit versions of the .NET Core SDK are installed. 只有安裝在 ' C: Program Files dotnet sdk ' 64 位版本的範本才 \ \ \ \ 會顯示。Only templates from the 64-bit versions installed at 'C:\Program Files\dotnet\sdk\' are displayed.

當安裝 .NET Core SDK 的32位 (x86) 和64位 (x64) 版本時,會出現此警告。This warning appears when both 32-bit (x86) and 64-bit (x64) versions of the .NET Core SDK are installed. 可能會安裝兩個版本的常見原因包括:Common reasons both versions may be installed include:

  • 您原本使用32位電腦下載了 .NET Core SDK 安裝程式,然後將它複製到64位電腦並安裝在位電腦上。You originally downloaded the .NET Core SDK installer using a 32-bit machine but then copied it across and installed it on a 64-bit machine.
  • 32位 .NET Core SDK 是由另一個應用程式安裝。The 32-bit .NET Core SDK was installed by another application.
  • 下載並安裝錯誤的版本。The wrong version was downloaded and installed.

卸載32位 .NET Core SDK 以防止此警告。Uninstall the 32-bit .NET Core SDK to prevent this warning. 主控台 的 [ > 程式和功能] 卸載 > 或變更程式Uninstall from Control Panel > Programs and Features > Uninstall or change a program. 如果您瞭解為什麼會發生警告和其含意,您可以忽略此警告。If you understand why the warning occurs and its implications, you can ignore the warning.

.NET Core SDK 安裝在多個位置The .NET Core SDK is installed in multiple locations

在 ASP.NET Core 的 [ 新增專案 ] 對話方塊中,您可能會看到下列警告:In the New Project dialog for ASP.NET Core, you may see the following warning:

.NET Core SDK 安裝在多個位置。The .NET Core SDK is installed in multiple locations. 只會顯示 [C: \ Program Files dotnet sdk] 安裝的 sdk 範本 \ \ \ 。Only templates from the SDKs installed at 'C:\Program Files\dotnet\sdk\' are displayed.

當您在 C: \ Program Files \ dotnet \ SDK \ 以外的目錄中至少有一個 .NET Core SDK 安裝時,就會看到此訊息。You see this message when you have at least one installation of the .NET Core SDK in a directory outside of C:\Program Files\dotnet\sdk\. 當 .NET Core SDK 已使用複製/貼上部署到電腦上,而不是使用 MSI 安裝程式時,通常就會發生這種情況。Usually this happens when the .NET Core SDK has been deployed on a machine using copy/paste instead of the MSI installer.

卸載所有32位 .NET Core Sdk 和執行時間以防止此警告。Uninstall all 32-bit .NET Core SDKs and runtimes to prevent this warning. 主控台 的 [ > 程式和功能] 卸載 > 或變更程式Uninstall from Control Panel > Programs and Features > Uninstall or change a program. 如果您瞭解為什麼會發生警告和其含意,您可以忽略此警告。If you understand why the warning occurs and its implications, you can ignore the warning.

未偵測到任何 .NET Core SdkNo .NET Core SDKs were detected

  • 在 ASP.NET Core 的 Visual Studio 新增專案 ] 對話方塊中,您可能會看到下列警告:In the Visual Studio New Project dialog for ASP.NET Core, you may see the following warning:

    未偵測到任何 .NET Core Sdk,請確認其包含在環境變數中 PATHNo .NET Core SDKs were detected, ensure they are included in the environment variable PATH.

  • 執行命令時 dotnet ,警告會顯示為:When executing a dotnet command, the warning appears as:

    找不到任何已安裝的 dotnet Sdk。It was not possible to find any installed dotnet SDKs.

當環境變數 PATH 未指向電腦上的任何 .Net Core sdk 時,就會出現這些警告。These warnings appear when the environment variable PATH doesn't point to any .NET Core SDKs on the machine. 若要解決此問題:To resolve this problem:

  • 安裝 .NET Core SDK。Install the .NET Core SDK. .Net 下載取得最新的安裝程式。Obtain the latest installer from .NET Downloads.
  • 確認 PATH 環境變數指向 C:\Program Files\dotnet\ 針對64位/x64 或 C:\Program Files (x86)\dotnet\ 32 位/x86) (安裝 SDK 的位置。Verify that the PATH environment variable points to the location where the SDK is installed (C:\Program Files\dotnet\ for 64-bit/x64 or C:\Program Files (x86)\dotnet\ for 32-bit/x86). SDK 安裝程式通常會設定 PATHThe SDK installer normally sets the PATH. 一律在同一部電腦上安裝相同的位 Sdk 和執行時間。Always install the same bitness SDKs and runtimes on the same machine.

安裝 .NET Core 裝載套件組合之後遺失 SDKMissing SDK after installing the .NET Core Hosting Bundle

安裝 .Net Core 裝載 套件組合時, PATH 會在安裝 .net core 執行時間以指向 .net core () 的 32 (x86) 版本時,修改 C:\Program Files (x86)\dotnet\Installing the .NET Core Hosting Bundle modifies the PATH when it installs the .NET Core runtime to point to the 32-bit (x86) version of .NET Core (C:\Program Files (x86)\dotnet\). 這可能會在使用32位 (x86) .NET Core dotnet 命令 (未偵測到任何 .Net core sdk) 時,導致缺少 sdk。This can result in missing SDKs when the 32-bit (x86) .NET Core dotnet command is used (No .NET Core SDKs were detected). 若要解決這個問題,請移 C:\Program Files\dotnet\ 至之前的位置 C:\Program Files (x86)\dotnet\ PATHTo resolve this problem, move C:\Program Files\dotnet\ to a position before C:\Program Files (x86)\dotnet\ on the PATH.

從應用程式取得資料Obtain data from an app

如果應用程式能夠回應要求,您可以使用中介軟體從應用程式取得下列資料:If an app is capable of responding to requests, you can obtain the following data from the app using middleware:

  • 要求:方法、配置、主機、pathbase、路徑、查詢字串、標頭Request: Method, scheme, host, pathbase, path, query string, headers
  • 連接:遠端 IP 位址、遠端埠、本機 IP 位址、本機埠、用戶端憑證Connection: Remote IP address, remote port, local IP address, local port, client certificate
  • Identity:名稱、顯示名稱Identity: Name, display name
  • 組態設定Configuration settings
  • 環境變數Environment variables

在方法的要求處理管線開頭放置下列 中介軟體 程式碼 Startup.ConfigurePlace the following middleware code at the beginning of the Startup.Configure method's request processing pipeline. 環境會在中介軟體執行之前檢查,以確保程式碼只會在開發環境中執行。The environment is checked before the middleware is run to ensure that the code is only executed in the Development environment.

若要取得環境,請使用下列其中一種方法:To obtain the environment, use either of the following approaches:

  • 將插入 IHostingEnvironmentStartup.Configure 方法中,並以本機變數檢查環境。Inject the IHostingEnvironment into the Startup.Configure method and check the environment with the local variable. 下列範例程式碼示範此方法。The following sample code demonstrates this approach.

  • 將環境指派至類別中的屬性 StartupAssign the environment to a property in the Startup class. 使用屬性 (來檢查環境,例如 if (Environment.IsDevelopment())) 。Check the environment using the property (for example, if (Environment.IsDevelopment())).

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
    IConfiguration config)
{
    if (env.IsDevelopment())
    {
        app.Run(async (context) =>
        {
            var sb = new StringBuilder();
            var nl = System.Environment.NewLine;
            var rule = string.Concat(nl, new string('-', 40), nl);
            var authSchemeProvider = app.ApplicationServices
                .GetRequiredService<IAuthenticationSchemeProvider>();

            sb.Append($"Request{rule}");
            sb.Append($"{DateTimeOffset.Now}{nl}");
            sb.Append($"{context.Request.Method} {context.Request.Path}{nl}");
            sb.Append($"Scheme: {context.Request.Scheme}{nl}");
            sb.Append($"Host: {context.Request.Headers["Host"]}{nl}");
            sb.Append($"PathBase: {context.Request.PathBase.Value}{nl}");
            sb.Append($"Path: {context.Request.Path.Value}{nl}");
            sb.Append($"Query: {context.Request.QueryString.Value}{nl}{nl}");

            sb.Append($"Connection{rule}");
            sb.Append($"RemoteIp: {context.Connection.RemoteIpAddress}{nl}");
            sb.Append($"RemotePort: {context.Connection.RemotePort}{nl}");
            sb.Append($"LocalIp: {context.Connection.LocalIpAddress}{nl}");
            sb.Append($"LocalPort: {context.Connection.LocalPort}{nl}");
            sb.Append($"ClientCert: {context.Connection.ClientCertificate}{nl}{nl}");

            sb.Append($"Identity{rule}");
            sb.Append($"User: {context.User.Identity.Name}{nl}");
            var scheme = await authSchemeProvider
                .GetSchemeAsync(IISDefaults.AuthenticationScheme);
            sb.Append($"DisplayName: {scheme?.DisplayName}{nl}{nl}");

            sb.Append($"Headers{rule}");
            foreach (var header in context.Request.Headers)
            {
                sb.Append($"{header.Key}: {header.Value}{nl}");
            }
            sb.Append(nl);

            sb.Append($"Websockets{rule}");
            if (context.Features.Get<IHttpUpgradeFeature>() != null)
            {
                sb.Append($"Status: Enabled{nl}{nl}");
            }
            else
            {
                sb.Append($"Status: Disabled{nl}{nl}");
            }

            sb.Append($"Configuration{rule}");
            foreach (var pair in config.AsEnumerable())
            {
                sb.Append($"{pair.Path}: {pair.Value}{nl}");
            }
            sb.Append(nl);

            sb.Append($"Environment Variables{rule}");
            var vars = System.Environment.GetEnvironmentVariables();
            foreach (var key in vars.Keys.Cast<string>().OrderBy(key => key, 
                StringComparer.OrdinalIgnoreCase))
            {
                var value = vars[key];
                sb.Append($"{key}: {value}{nl}");
            }

            context.Response.ContentType = "text/plain";
            await context.Response.WriteAsync(sb.ToString());
        });
    }
}

ASP.NET Core 應用程式的偵錯工具Debug ASP.NET Core apps

下列連結提供有關 ASP.NET Core 應用程式的偵錯工具的資訊。The following links provide information on debugging ASP.NET Core apps.