疑難排解 ASP.NET Core 專案Troubleshoot ASP.NET Core projects

作者:Rick AndersonBy Rick Anderson

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

.NET core SDK 警告.NET Core SDK warnings

已安裝32位和64位版本的 .NET Core SDKBoth 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:

已安裝32位和64位版本的 .NET Core SDK。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.

時,會出現這個警告 (x86) 32 位元和 64 位元 (x64) 版本的.NET Core SDK安裝。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:

  • 您原本下載.NET Core SDK 安裝程式使用 32 位元電腦,但然後複製其整個並安裝在 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.

您以外的目錄中有至少一個安裝的.NET Core sdk 時,您會看到此訊息c:\Program Files\dotnet\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\. 通常這會使用複製/貼上,而不 MSI 安裝程式在機器上部署.NET Core SDK。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, 請確認其包含在環境變數PATH中。No .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.
  • 確認環境變數指向安裝 SDK 的位置 (C:\Program Files\dotnet\適用于64位/x64 或C:\Program Files (x86)\dotnet\ 32 位/x86)。 PATHVerify 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\ PATH之前的位置。To 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:

  • Request –方法、配置、主機、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 – Name, display name
  • 組態設定Configuration settings
  • 環境變數Environment variables

將下列中介軟體程式碼放在Startup.Configure方法的要求處理管線的開頭。Place 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:

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

  • 將環境指派給Startup類別中的屬性。Assign 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());
        });
    }
}