從 ASP.NET Core 2.0 來 2.1 移轉Migrate from ASP.NET Core 2.0 to 2.1

作者:Rick AndersonBy Rick Anderson

請參閱ASP.NET Core 2.1 中最新消息如需 ASP.NET Core 2.1 中的新功能的概觀。See What's new in ASP.NET Core 2.1 for an overview of the new features in ASP.NET Core 2.1.

這篇文章:This article:

  • 涵蓋基本的 ASP.NET Core 2.0 應用程式移轉為 2.1。Covers the basics of migrating an ASP.NET Core 2.0 app to 2.1.
  • 提供 ASP.NET Core web 應用程式範本所做的變更的概觀。Provides an overview of the changes to the ASP.NET Core web application templates.

取得 2.1 中的變更概觀的快速方法是:A quick way to get an overview of the changes in 2.1 is to:

  • 建立名為 WebApp1 的 ASP.NET Core 2.0 web 應用程式。Create an ASP.NET Core 2.0 web app named WebApp1.
  • 認可 WebApp1 的原始檔控制系統中。Commit the WebApp1 in a source control system.
  • 刪除 WebApp1 並建立名為 WebApp1 的相同位置中的 ASP.NET Core 2.1 web 應用程式。Delete WebApp1 and create an ASP.NET Core 2.1 web app named WebApp1 in the same place.
  • 檢閱在 2.1 版的變更。Review the changes in the 2.1 version.

這篇文章提供有關移轉至 ASP.NET Core 2.1 的概觀。This article provides an overview on migration to ASP.NET Core 2.1. 它不包含移轉至版本 2.1 所需的所有變更的完整清單。It doesn't contain a complete list of all changes needed to migrate to version 2.1. 有些專案可能需要更多的步驟,根據建立專案時選取的選項和專案所做的修改。Some projects might require more steps depending on the options selected when the project was created and modifications made to the project.

更新要使用 2.1 版的專案檔Update the project file to use 2.1 versions

更新專案檔:Update the project file:

  • 將目標 framework 變更為.NET Core 2.1 中,藉由更新專案檔,以<TargetFramework>netcoreapp2.1</TargetFramework>Change the target framework to .NET Core 2.1 by updating the project file to <TargetFramework>netcoreapp2.1</TargetFramework>.
  • 取代的套件參考Microsoft.AspNetCore.All使用的套件參考Microsoft.AspNetCore.AppReplace the package reference for Microsoft.AspNetCore.All with a package reference for Microsoft.AspNetCore.App. 您可能需要新增相依性,移除了Microsoft.AspNetCore.AllYou may need to add dependencies that were removed from Microsoft.AspNetCore.All. 如需詳細資訊,請參閱 ASP.NET Core 2.0 的 Microsoft.AspNetCore.All 中繼套件ASP.NET Core 2.1 或更新版本的 Microsoft.AspNetCore.App 中繼套件For more information, see ASP.NET Core 2.0 的 Microsoft.AspNetCore.All 中繼套件 and ASP.NET Core 2.1 或更新版本的 Microsoft.AspNetCore.App 中繼套件.
  • 移除的套件參考的 「 版本 」 屬性Microsoft.AspNetCore.AppRemove the "Version" attribute on the package reference to Microsoft.AspNetCore.App. 專案使用<Project Sdk="Microsoft.NET.Sdk.Web">不需要設定版本。Projects that use <Project Sdk="Microsoft.NET.Sdk.Web"> don't need to set the version. 版本是隱含目標架構,並選取最能符合 ASP.NET Core 2.1 的運作的方式。The version is implied by the target framework and selected to best match the way ASP.NET Core 2.1 works. 如需詳細資訊,請參閱 < 規則,以共用的 framework 為目標的專案一節。For more information, see the Rules for projects targeting the shared framework section.
  • 針對.NET Framework 為目標的應用程式,更新 2.1 的每個封裝參考。For apps that target the .NET Framework, update each package reference to 2.1.
  • 移除參考 <DotNetCliToolReference> 下列封裝的項目。Remove references to <DotNetCliToolReference> elements for the following packages. 這些工具會配套在預設會在.NET Core CLI,並不需要另外安裝。These tools are bundled by default in the .NET Core CLI and don't need to be installed separately.
    • Microsoft.DotNet.Watcher.Tools (dotnet watch)Microsoft.DotNet.Watcher.Tools (dotnet watch)
    • Microsoft.EntityFrameworkCore.Tools.DotNet (dotnet ef)Microsoft.EntityFrameworkCore.Tools.DotNet (dotnet ef)
    • Microsoft.Extensions.Caching.SqlConfig.Tools (dotnet sql-cache)Microsoft.Extensions.Caching.SqlConfig.Tools (dotnet sql-cache)
    • Microsoft.Extensions.SecretManager.Tools (dotnet user-secrets)Microsoft.Extensions.SecretManager.Tools (dotnet user-secrets)
  • 選擇性: 您可以移除 <DotNetCliToolReference> 項目Microsoft.VisualStudio.Web.CodeGeneration.ToolsOptional: you can remove the <DotNetCliToolReference> element for Microsoft.VisualStudio.Web.CodeGeneration.Tools. 您可以使用全域安裝的版本取代這項工具,藉由執行dotnet tool install -g dotnet-aspnet-codegeneratorYou can replace this tool with a globally installed version by running dotnet tool install -g dotnet-aspnet-codegenerator.
  • 針對 2.1 版, Razor 類別庫是將 Razor 檔案的建議的解決方案。For 2.1, a Razor Class Library is the recommended solution to distribute Razor files. 如果您的應用程式會使用內嵌的檢視,或依賴執行階段編譯 Razor 檔案,否則新增<CopyRefAssembliesToPublishDirectory>true</CopyRefAssembliesToPublishDirectory><PropertyGroup>專案檔中。If your app uses embedded views, or otherwise relies on runtime compilation of Razor files, add <CopyRefAssembliesToPublishDirectory>true</CopyRefAssembliesToPublishDirectory> to a <PropertyGroup> in your project file.

下列標記會顯示範本所產生的 2.0 專案檔案:The following markup shows the template-generated 2.0 project file:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <UserSecretsId>aspnet-{Project Name}-{GUID}</UserSecretsId>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.3" PrivateAssets="All" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.4" PrivateAssets="All" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.2" />
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
  </ItemGroup>
</Project>

下列標記會顯示範本產生 2.1 的專案檔:The following markup shows the template-generated 2.1 project file:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <UserSecretsId>aspnet-{Project Name}-{GUID}</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" PrivateAssets="All" />
  </ItemGroup>

</Project>

共用的架構為目標的專案的規則Rules for projects targeting the shared framework

「共用的架構」 是一組不在應用程式資料夾內的組件 ( .dll 檔案)。A shared framework is a set of assemblies (.dll files) that are not in the app's folders. 共用的架構必須安裝於要執行應用程式的機器上。The shared framework must be installed on the machine to run the app. 如需詳細資訊,請參閱共用的架構 (英文)。For more information, see The shared framework.

ASP.NET Core 2.1 包含下列的共用的架構:ASP.NET Core 2.1 includes the following shared frameworks:

指定的套件參考的版本是至少需要版本。The version specified by the package reference is the minimum required version. 例如,專案參考這些套件的版本將不會只 2.1.0 的電腦執行 2.1.1 執行階段安裝。For example, a project referencing the 2.1.1 versions of these packages won't run on a machine with only the 2.1.0 runtime installed.

以共用的架構為目標的專案的已知的問題:Known issues for projects targeting a shared framework:

  • .NET Core SDK (第一次隨附於 Visual Studio 15.6) 設定的隱含版本 2.1.300Microsoft.AspNetCore.App來使用 Entity Framework Core 2.1.1 導致衝突 2.1.0。The .NET Core 2.1.300 SDK (first included in Visual Studio 15.6) set the implicit version of Microsoft.AspNetCore.App to 2.1.0 which caused conflicts with Entity Framework Core 2.1.1. 建議的解決方案是升級至 2.1.301 或更新版本時,.NET Core SDK。The recommended solution is to upgrade the .NET Core SDK to 2.1.301 or later. 如需詳細資訊,請參閱 < Microsoft.AspNetCore.App 與共用相依性的套件不能參考修補程式版本For more information, see Packages that share dependencies with Microsoft.AspNetCore.App cannot reference patch versions.

  • 必須使用的所有專案Microsoft.AspNetCore.All或是Microsoft.AspNetCore.App應該在專案檔中,新增套件參考封裝,即使它們包含另一個專案使用的專案參考Microsoft.AspNetCore.AllMicrosoft.AspNetCore.AppAll projects that must use Microsoft.AspNetCore.All or Microsoft.AspNetCore.App should add a package reference for the package in the project file, even if they contain a project reference to another project using Microsoft.AspNetCore.All or Microsoft.AspNetCore.App.

    範例:Example:

    • MyApp 有的套件參考Microsoft.AspNetCore.AppMyApp has a package reference to Microsoft.AspNetCore.App.
    • MyApp.Tests 專案參考MyApp.csprojMyApp.Tests has a project reference to MyApp.csproj.

    新增的套件參考Microsoft.AspNetCore.AppMyApp.TestsAdd a package reference for Microsoft.AspNetCore.App to MyApp.Tests. 如需詳細資訊,請參閱 < 整合測試很難設定,並可能會中斷上共用的架構服務For more information, see Integration testing is hard to set up and may break on shared framework servicing.

更新為 2.1 的 Docker 映像Update to the 2.1 Docker images

在 ASP.NET Core 2.1 中的 Docker 映像移轉到dotnet/dotnet-docker GitHub 存放庫In ASP.NET Core 2.1, the Docker images migrated to the dotnet/dotnet-docker GitHub repository. 下表顯示 Docker 映像和標記的變更:The following table shows the Docker image and tag changes:

2.02.0 2.12.1
microsoft/aspnetcore:2.0microsoft/aspnetcore:2.0 microsoft/dotnet:2.1-aspnetcore-runtimemicrosoft/dotnet:2.1-aspnetcore-runtime
microsoft/aspnetcore-build:2.0microsoft/aspnetcore-build:2.0 microsoft/dotnet:2.1-sdkmicrosoft/dotnet:2.1-sdk

變更FROM中您Dockerfile 2.1 上表的資料行中使用新的映像名稱和標記。Change the FROM lines in your Dockerfile to use the new image names and tags in the preceding table's 2.1 column. 如需詳細資訊,請參閱 < aspnetcore docker 存放庫從移轉至 dotnetFor more information, see Migrating from aspnetcore docker repos to dotnet.

主要的變更Changes to Main

下圖顯示產生的範本所做的變更Program.cs檔案。The following images show the changes made to the templated generated Program.cs file.

舊的版本差異

上圖顯示以紅色的 2.0 版與刪除。The preceding image shows the 2.0 version with the deletions in red.

下圖顯示 2.1 的程式碼。The following image shows the 2.1 code. 以綠色顯示的程式碼取代 2.0 版:The code in green replaced the 2.0 version:

新的版本差異

下列程式碼顯示 2.1 版Program.cs:The following code shows the 2.1 version of Program.cs:

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

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
}

Main會取代呼叫BuildWebHost具有CreateWebHostBuilderThe new Main replaces the call to BuildWebHost with CreateWebHostBuilder. IWebHostBuilder已加入來支援新整合測試基礎結構IWebHostBuilder was added to support a new integration test infrastructure.

若要啟動的變更Changes to Startup

下列程式碼顯示 2.1 範本產生程式碼變更。The following code shows the changes to 2.1 template generated code. 所有的變更會新增的程式碼中的,不同之處在於UseBrowserLink已移除:All changes are newly added code, except that UseBrowserLink has been removed:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace WebApp1
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            // If the app uses Session or TempData based on Session:
            // app.UseSession();

            app.UseMvc();
        }
    }
}

上述程式碼變更的詳細資訊,請說明:The preceding code changes are detailed in:

驗證程式碼變更Changes to authentication code

提供 ASP.NET Core 2.1 ASP.NET Core Identity作為Razor 類別庫(RCL)。ASP.NET Core 2.1 provides ASP.NET Core Identity as a Razor Class Library (RCL).

預設 2.1 識別 UI 不目前提供重要的新功能透過 2.0 版。The default 2.1 Identity UI doesn't currently provide significant new features over the 2.0 version. 取代 RCL 封裝中的身分識別是選擇性的。Replacing Identity with the RCL package is optional. 若要取代範本的優點產生 RCL 版本的程式碼包含身分識別:The advantages to replacing the template generated Identity code with the RCL version include:

  • 許多檔案會移出您的來源樹狀目錄中。Many files are moved out of your source tree.
  • 中包含的任何 bug 修正或身分識別的新功能Microsoft.AspNetCore.App 中繼套件Any bug fixes or new features to Identity are included in the Microsoft.AspNetCore.App metapackage. 您可以自動取得更新的身分識別時Microsoft.AspNetCore.App會更新。You automatically get the updated Identity when Microsoft.AspNetCore.App is updated.

如果您已擬定非一般範本的變更會產生身分識別程式碼:If you've made non-trivial changes to the template generated Identity code:

  • 上述的優點可能不要左右對齊轉換成 RCL 版本。The preceding advantages probably do not justify converting to the RCL version.
  • 您可以保留您的 ASP.NET Core 2.0 身分識別程式碼,它完整支援。You can keep your ASP.NET Core 2.0 Identity code, it's fully supported.

身分識別 2.1 會公開端點Identity區域。Identity 2.1 exposes endpoints with the Identity area. 比方說下, 表顯示從 2.0 變更為 2.1 的身分識別端點的範例:For example, the follow table shows examples of Identity endpoints that change from 2.0 to 2.1:

2.0 URL2.0 URL 2.1 URL2.1 URL
/ 帳戶/登入/Account/Login / 身分識別/帳戶/登入/Identity/Account/Login
/Account/Logout/Account/Logout / 身分識別/帳戶/登出/Identity/Account/Logout
/ 帳戶/管理/Account/Manage / 身分識別/帳戶/管理/Identity/Account/Manage

使用身分識別的程式碼,並取代 2.0 的應用程式的 2.1 的身分識別程式庫需求納入考量的識別 Url 的身分識別 UI 有/IdentityUri 的前面加上的區段。Applications that have code using Identity and replace 2.0 Identity UI with the 2.1 Identity Library need to take into account Identity URLs have /Identity segment prepended to the URIs. 一種方式來處理新身分識別端點是從 設定重新導向,例如/Account/Login/Identity/Account/LoginOne way to handle the new Identity endpoints is to set up redirects, for example from /Account/Login to /Identity/Account/Login.

2.1 版的更新身分識別Update Identity to version 2.1

下列選項可更新為 2.1 的身分識別。The following options are available to update Identity to 2.1.

  • 使用身分識別 UI 2.0 程式碼,而不需變更。Use the Identity UI 2.0 code with no changes. 完全支援使用識別 UI 2.0 程式碼。Using Identity UI 2.0 code is fully supported. 重大變更已對產生的身分識別程式碼時,這會是個不錯的方法。This is a good approach when significant changes have been made to the generated Identity code.
  • 刪除現有的身分識別 2.0 程式碼並Scaffold 識別到您的專案。Delete your existing Identity 2.0 code and Scaffold Identity into your project. 您的專案將會使用ASP.NET Core Identity Razor 類別庫Your project will use the ASP.NET Core Identity Razor Class Library. 您可以產生程式碼和任何您已修改的身分識別 UI 程式碼的 UI。You can generate code and UI for any of the Identity UI code that you modified. 將程式碼變更套用到新的 scaffold 的 UI 程式碼。Apply your code changes to the newly scaffolded UI code.
  • 刪除現有的身分識別 2.0 程式碼並Scaffold 身分識別到您的專案,選擇覆寫所有檔案Delete your existing Identity 2.0 code and Scaffold Identity into your project with the option to Override all files.

取代識別 2.0 身分識別 2.1 Razor 類別庫的 UIReplace Identity 2.0 UI with the Identity 2.1 Razor Class Library

本節概述的步驟,來取代 ASP.NET Core 2.0 範本產生身分識別程式碼,使用ASP.NET Core Identity Razor 類別庫This section outlines the steps to replace the ASP.NET Core 2.0 template generated Identity code with the ASP.NET Core Identity Razor Class Library. 下列步驟適用於 Razor Pages 專案,但是 MVC 專案中的方法是類似。The following steps are for a Razor Pages project, but the approach for an MVC project is similar.

  • 確認專案檔更新為使用 2.1 版Verify the project file is updated to use 2.1 versions
  • 刪除下列資料夾中的所有檔案:Delete the following folders and all the files in them:
    • 控制器Controllers
    • Pages/Account/Pages/Account/
    • 延伸模組Extensions
  • 建置專案。Build the project.
  • 建立身分識別的結構到您的專案:Scaffold Identity into your project:
    • 選取現有的專案 _Layout.cshtml檔案。Select the projects exiting _Layout.cshtml file.
    • 選取 + 右邊的圖示資料內容類別Select the + icon on the right side of the Data context class. 接受預設名稱。Accept the default name.
    • 選取 新增來建立新的資料內容類別。Select Add to create a new Data context class. 若要建立的結構,需要建立新的資料內容。Creating a new data context is required for to scaffold. 您在下一節中移除新的資料內容。You remove the new data context in the next section.

更新之後 scaffolding 身分識別Update after scaffolding Identity

  • 刪除產生的身分識別 scaffolderIdentityDbContext衍生類別中的區域/身分識別/Data/ 資料夾。Delete the Identity scaffolder generated IdentityDbContext derived class in the Areas/Identity/Data/ folder.

  • 刪除Areas/Identity/IdentityHostingStartup.csDelete Areas/Identity/IdentityHostingStartup.cs.

  • 更新 _LoginPartial.cshtml檔案:Update the _LoginPartial.cshtml file:

    • 移動頁 /_LoginPartial.cshtml共用頁面 / /_LoginPartial.cshtmlMove Pages/_LoginPartial.cshtml to Pages/Shared/_LoginPartial.cshtml.
    • 新增asp-area="Identity"表單和錨點連結。Add asp-area="Identity" to the form and anchor links.
    • 更新<form />項目<form asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Page("/Index", new { area = "" })" method="post" id="logoutForm" class="navbar-right">Update the <form /> element to <form asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Page("/Index", new { area = "" })" method="post" id="logoutForm" class="navbar-right">.

    下列程式碼顯示已更新 _LoginPartial.cshtml檔案:The following code shows the updated _LoginPartial.cshtml file:

    @using Microsoft.AspNetCore.Identity
    
    @inject SignInManager<ApplicationUser> SignInManager
    @inject UserManager<ApplicationUser> UserManager
    
    @if (SignInManager.IsSignedIn(User))
    {
        <form asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Page("/Index", new { area = "" })" method="post" id="logoutForm" class="navbar-right">
            <ul class="nav navbar-nav navbar-right">
                <li>
                    <a asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
                </li>
                <li>
                    <button type="submit" class="btn btn-link navbar-btn navbar-link">Log out</button>
                </li>
            </ul>
        </form>
    }
    else
    {
        <ul class="nav navbar-nav navbar-right">
            <li><a asp-area="Identity" asp-page="/Account/Register">Register</a></li>
            <li><a asp-area="Identity" asp-page="/Account/Login">Log in</a></li>
        </ul>
    }
    

更新ConfigureServices為下列程式碼:Update ConfigureServices with the following code:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddDefaultIdentity<ApplicationUser>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    services.AddMvc();

    // Register no-op EmailSender used by account confirmation and password reset 
    // during development
    services.AddSingleton<IEmailSender, EmailSender>();
}

變更至 Razor Pages 專案 Razor 檔案Changes to Razor Pages projects Razor files

版面配置檔案The layout file

  • 移動頁 /_Layout.cshtml頁/Shared/_Layout.cshtmlMove Pages/_Layout.cshtml to Pages/Shared/_Layout.cshtml

  • 領域/身分識別/頁面/_ViewStart.cshtml,變更Layout = "/Pages/_Layout.cshtml"Layout = "/Pages/Shared/_Layout.cshtml"In Areas/Identity/Pages/_ViewStart.cshtml, change Layout = "/Pages/_Layout.cshtml" to Layout = "/Pages/Shared/_Layout.cshtml".

  • _Layout.cshtml檔案有下列變更:The _Layout.cshtml file has the following changes:

    • <partial name="_CookieConsentPartial" /> 會加入。<partial name="_CookieConsentPartial" /> is added. 如需詳細資訊,請參閱 ASP.NET Core 的 GDPR 支援For more information, see GDPR support in ASP.NET Core.
    • jQuery 的 2.2.0 3.3.1 變更。jQuery changes from 2.2.0 to 3.3.1.

_ValidationScriptsPartial.cshtml_ValidationScriptsPartial.cshtml

  • 頁面 /_ValidationScriptsPartial.cshtml頁/Shared/_ValidationScriptsPartial.cshtmlPages/_ValidationScriptsPartial.cshtml moves to Pages/Shared/_ValidationScriptsPartial.cshtml.
  • jquery.validate/1.14.0變更為jquery.validate/1.17.0jquery.validate/1.14.0 changes to jquery.validate/1.17.0.

新的檔案New files

加入下列檔案:The following files are added:

  • Privacy.cshtmlPrivacy.cshtml
  • Privacy.cshtml.csPrivacy.cshtml.cs

請參閱ASP.NET Core 中的 GDPR 支援有關上述檔案。See GDPR support in ASP.NET Core for information on the preceding files.

MVC 專案 Razor 檔案的變更Changes to MVC projects Razor files

版面配置檔案The layout file

Layout.cshtml檔案有下列變更:The Layout.cshtml file has the following changes:

  • <partial name="_CookieConsentPartial" /> 會加入。<partial name="_CookieConsentPartial" /> is added.
  • jQuery 從 2.2.0 變成 3.3.1jQuery changes from 2.2.0 to 3.3.1

_ValidationScriptsPartial.cshtml_ValidationScriptsPartial.cshtml

jquery.validate/1.14.0變更為jquery.validate/1.17.0jquery.validate/1.14.0 changes to jquery.validate/1.17.0

新的檔案和動作方法New files and action methods

中加入下列內容:The following are added:

  • Views/Home/Privacy.cshtmlViews/Home/Privacy.cshtml
  • Privacy動作方法會加入至主控制器。The Privacy action method is added to the Home controller.

請參閱ASP.NET Core 中的 GDPR 支援有關上述檔案。See GDPR support in ASP.NET Core for information on the preceding files.

LaunchSettings.json 檔案的變更Changes to the launchSettings.json file

根據預設,ASP.NET Core 應用程式現在使用的 HTTPS Properties/launchSettings.json檔案已變更。As ASP.NET Core apps now use HTTPS by default, the Properties/launchSettings.json file has changed.

下列 JSON 顯示範本所產生的較早 2.0 launchSettings.json檔案:The following JSON shows the earlier 2.0 template-generated launchSettings.json file:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:1799/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApp1": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:1798/"
    }
  }
}

下列 JSON 顯示範本產生新的 2.1 launchSettings.json檔案:The following JSON shows the new 2.1 template-generated launchSettings.json file:

{
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:39191",
      "sslPort": 44390
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApp1": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

如需詳細資訊,請參閱 強制使用 ASP.NET Core 中的 HTTPSFor more information, see 強制使用 ASP.NET Core 中的 HTTPS.

重大變更Breaking changes

FileResult 範圍標頭FileResult Range header

FileResult 不會再處理Accept-ranges預設標頭。FileResult no longer processes the Accept-Ranges header by default. 若要啟用Accept-Ranges標頭,設定EnableRangeProcessingtrueTo enable the Accept-Ranges header, set EnableRangeProcessing to true.

ControllerBase.File 和 PhysicalFile 範圍標頭ControllerBase.File and PhysicalFile Range header

下列ControllerBase方法不會再處理Accept-ranges預設標頭:The following ControllerBase methods no longer processes the Accept-Ranges header by default:

若要啟用Accept-Ranges標頭,設定EnableRangeProcessing參數來trueTo enable the Accept-Ranges header, set the EnableRangeProcessing parameter to true.

其他變更Additional changes