如何將 ASP.NET MVC 4 和 Web API 專案升級至 ASP.NET MVC 5 和 Web API 2How to Upgrade an ASP.NET MVC 4 and Web API Project to ASP.NET MVC 5 and Web API 2

Rick Andersonby Rick Anderson

ASP.NET MVC 5 和 Web API 2 將新功能,包括路由屬性、 驗證篩選條件,以及執行更多的主機。ASP.NET MVC 5 and Web API 2 bring a host of new features, including attribute routing, authentication filters, and much more. 請參閱 https://www.asp.net/vnext 如需詳細資訊。See https://www.asp.net/vnext for more details.

本逐步解說會引導您升級為最新版本的應用程式所需的步驟。This walkthrough will guide you with the steps required to upgrade your application to the latest version.

注意

請參閱ASP.NET 及 Web Tools for Visual Studio 2013 版本資訊的重大變更到下一個版本 MVC 4 和 Web API 的詳細資訊。Please see ASP.NET and Web Tools for Visual Studio 2013 Release Notes for information on breaking changes from MVC 4 and Web API to the next version.

本文撰寫 Youngjune 香港和 Rick Anderson ( @RickAndMSFT )This article was written by Youngjune Hong and Rick Anderson ( @RickAndMSFT )

升級步驟Upgrade Steps

  1. 備份您的專案。Backup your project. 本逐步解說將會要求您對您的專案檔、 封裝組態和 web.config 檔案中的變更。This walkthrough will require you to make changes to your project file, package configuration, and web.config files.

  2. 在 global.asax 中, 升級從 Web API Web API 2,變更:For upgrading from Web API to Web API 2, in global.asax, change:

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    

    設為to

    GlobalConfiguration.Configure(WebApiConfig.Register);
    
  3. 請確定您的專案使用的所有封裝都都與 MVC 5 和 Web API 2 相容。Make sure all the packages that your projects use are compatible with MVC 5 and Web API 2. 下列表格顯示 MVC 4 和 Web API 相關封裝比需要變更。The following table shows the MVC 4 and Web API related packages than need to be changed. 如果您有相依於其中一個下面所列封裝的封裝,請連絡發行者以取得與 MVC 5 和 Web API 2 相容的較新版本。If you have a package that is dependent on one of the packages listed below, please contact the publishers to get the newer versions that are compatible with MVC 5 and Web API 2. 如果您有這些封裝的原始程式碼,則應該使用新的組件的 MVC 5 和 Web API 2 重新編譯它們。If you have the source code for those packages, you should recompile them with the new assemblies of MVC 5 and Web API 2.

    封裝識別碼Package Id 舊的版本Old version 新的版本New version
    Microsoft.AspNet.RazorMicrosoft.AspNet.Razor 2.0.x.x2.0.x.x 3.0.03.0.0
    Microsoft.AspNet.WebPagesMicrosoft.AspNet.WebPages 2.0.x.x2.0.x.x 3.0.03.0.0
    Microsoft.AspNet.WebPages.WebDataMicrosoft.AspNet.WebPages.WebData 2.0.x.x2.0.x.x 3.0.03.0.0
    Microsoft.AspNet.WebPages.OAuthMicrosoft.AspNet.WebPages.OAuth 2.0.x.x2.0.x.x 3.0.03.0.0
    Microsoft.AspNet.MvcMicrosoft.AspNet.Mvc 4.0.x.x4.0.x.x 5.0.05.0.0
    Microsoft.AspNet.Mvc.FacebookMicrosoft.AspNet.Mvc.Facebook 4.0.x.x4.0.x.x 5.0.05.0.0
    Microsoft.AspNet.WebApi.CoreMicrosoft.AspNet.WebApi.Core 4.0.x.x4.0.x.x 5.0.05.0.0
    Microsoft.AspNet.WebApi.SelfHostMicrosoft.AspNet.WebApi.SelfHost 4.0.x.x4.0.x.x 5.0.05.0.0
    Microsoft.AspNet.WebApi.ClientMicrosoft.AspNet.WebApi.Client 4.0.x.x4.0.x.x 5.0.05.0.0
    Microsoft.AspNet.WebApi.ODataMicrosoft.AspNet.WebApi.OData 4.0.x.x4.0.x.x 5.0.05.0.0
    Microsoft.AspNet.WebApiMicrosoft.AspNet.WebApi 4.0.x.x4.0.x.x 5.0.05.0.0
    Microsoft.AspNet.WebApi.WebHostMicrosoft.AspNet.WebApi.WebHost 4.0.x.x4.0.x.x 5.0.05.0.0
    Microsoft.AspNet.WebApi.TracingMicrosoft.AspNet.WebApi.Tracing 4.0.x.x4.0.x.x 5.0.05.0.0
    Microsoft.AspNet.WebApi.HelpPageMicrosoft.AspNet.WebApi.HelpPage 4.0.x.x4.0.x.x 5.0.05.0.0
    Microsoft.Net.HttpMicrosoft.Net.Http 2.0.x.2.0.x. 2.2.x.2.2.x.
    Microsoft.Data.ODataMicrosoft.Data.OData 5.2.x5.2.x 5.6.x5.6.x
    System.SpatialSystem.Spatial 5.2.x5.2.x 5.6.x5.6.x
    Microsoft.Data.EdmMicrosoft.Data.Edm 5.2.x5.2.x 5.6.x5.6.x
    Microsoft.AspNet.Mvc.FixedDisplayModesMicrosoft.AspNet.Mvc.FixedDisplayModes <o:p> </o:p><o:p> </o:p> 已移除Removed
    Microsoft.AspNet.WebPages.AdministrationMicrosoft.AspNet.WebPages.Administration <o:p> </o:p><o:p> </o:p> 已移除Removed
    Microsoft-Web-HelpersMicrosoft-Web-Helpers <o:p> </o:p><o:p> </o:p> Microsoft.AspNet.WebHelpersMicrosoft.AspNet.WebHelpers

    注意

    Microsoft Web Helpers Microsoft.AspNet.WebHelpers 已取代。Microsoft-Web-Helpers has been replaced with Microsoft.AspNet.WebHelpers. 您應該先移除舊的封裝,然後再安裝較新的封裝。You should remove the old package first, and then install the newer package.

    沒有跨版本之間的相容性主要 ASP.NET 封裝。There is no cross version compatibility among major ASP.NET packages. 例如,MVC 5 是只使用 Razor 3 和 Razor 2 不相容。For example, MVC 5 is compatible with only Razor 3, and not Razor 2.

  4. Visual Studio 2013 中開啟您的專案。Open your project in Visual Studio 2013.

  5. 移除任何已安裝下列 ASP.NET NuGet 套件。Remove any of the following ASP.NET NuGet packages that are installed. 您將會移除這些使用封裝管理員主控台 (PMC)。You will remove these using the Package Manager Console (PMC). 若要開啟 PMC,請選取工具功能表,然後選取程式庫封裝管理員,然後選取Package Manager ConsoleTo open the PMC, select the Tools menu and then select Library Package Manager, then select Package Manager Console. 您的專案可能不包括所有的這些。Your project might not include all of these.

    1. Microsoft.AspNet.WebPages.Administration
      從 MVC 3 升級至 MVC 4 時,通常會將加入此封裝。This package is typically added when upgrading from MVC 3 to MVC 4. 若要移除它,請在 PMC 中執行下列命令:To remove it, run the following command in the PMC:
      Uninstall-Package -Id Microsoft.AspNet.WebPages.Administration
    2. Microsoft-Web-Helpers
      此套件有品牌已重新命名為Microsoft.AspNet.WebHelpersThis package has been rebranded as Microsoft.AspNet.WebHelpers. 若要移除它,請在 PMC 中執行下列命令:To remove it, run the following command in the PMC:
      Uninstall-Package -Id Microsoft-Web-Helpers
    3. Microsoft.AspNet.Mvc.FixedDisplayMode
      此套件包含因應措施的 MVC 5 中已修正問題的 MVC 4 中的 bug。This package contains a work around for a bug in MVC 4 that has been fixed in MVC 5. 若要移除它,請在 PMC 中執行下列命令:To remove it, run the following command in the PMC:
      Uninstall-Package -Id Microsoft.AspNet.Mvc.FixedDisplayModes
  6. 升級使用 pmc 依存的所有 ASP.NET NuGet 封裝。Upgrade all the ASP.NET NuGet packages using the PMC. 在 PMC,執行下列命令:In the PMC, run the following command:
    Update-Package
    Update-Package命令沒有任何參數將會更新每個套件。The Update-Package command without any parameters will update every package. 您可以使用識別碼引數,個別更新封裝。You can update packages individually by using the ID argument. 如需有關更新命令的詳細資訊,請執行get-help update-packageFor more information about the update command, run get-help update-package .

更新應用程式web.config檔案Update the Application web.config File

請務必在應用程式中進行這些變更web.config檔案無法web.config檔案檢視資料夾。Be sure to make these changes in the app web.config file, not the web.config file in the Views folder.

找出<runtime>/<assemblyBinding>區段,然後進行下列變更:Locate the <runtime>/<assemblyBinding> section, and make the following changes:

  1. 在名稱屬性為"system.web.mvc 的參考 」 的項目,變更從 「 4.0.0.0"到"5.0.0.0 」 版本數字。In the elements with the name attribute "System.Web.Mvc", change the version number from "4.0.0.0" to "5.0.0.0". (該元素中的兩個變更。)(Two changes in that element.)

  2. 在具有 name 屬性的項目"System.Web.Helpers"和"System.Web.WebPages"變更從 「 2.0.0.0"的版本號碼,以 「 3.0.0.0"。In elements with the name attribute "System.Web.Helpers" and "System.Web.WebPages" change the version number from "2.0.0.0" to "3.0.0.0". 四項變更不會進行兩個在每個項目。Four changes will occur, two in each of the elements.

    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <!--Two elements removed for Clarity -->
        <dependentAssembly>
          <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
          <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
          <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
          <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
        </dependentAssembly>
        <!--WebGrease element removed for Clarity -->
      </assemblyBinding
    
  3. 找出<appSettings>區段,並更新從 2.0.0.0.0 webpages:version 至 3.0.0.0,如下所示:Locate the <appSettings> section and update the webpages:version from 2.0.0.0.0 to 3.0.0.0 as shown below:

    <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="PreserveLoginUrl" value="true" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
      </appSettings>
    
  4. 移除任何不完整的信任層級。Remove any trust levels other than Full. 例如: For example:

    <securityPolicy>
       <!--<trustLevel name="Medium"  policyFile="web_mediumtrust.config"/>-->
    </securityPolicy>
    

更新web.config [檢視] 資料夾下的檔案Update the web.config files under the Views folder

如果您的應用程式使用的區域,您也必須更新每個web.config檔案檢視子資料夾的每個區域的資料夾。If your application is using areas, you will also need to update each web.config file in the Views sub-folder of each Area folder.

  1. 更新包含版本 」 4.0.0.0"版本"5.0.0.0"到"system.web.mvc 的參考 」 的所有項目。Update all elements that contain "System.Web.Mvc" from version "4.0.0.0" to version "5.0.0.0".

    <system.web.webPages.razor>
      <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
          <add namespace="System.Web.Mvc" />
          <!--Elements removed for Clarity.-->
        </namespaces>
      </pages>
    </system.web.webPages.razor>
    
    -->
      <pages
          validateRequest="false"
          pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
          pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
          userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <controls>
          <add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
        </controls>
      </pages>
    </system.web>
    
  2. 更新包含 「 2.0.0.0"版版本 3.0.0.0 「 開始 」 到 「 System.Web.WebPages.Razor 」 的所有項目。Update all elements that contain "System.Web.WebPages.Razor" from version "2.0.0.0" to version"3.0.0.0". 如果這個區段包含 「 System.Web.WebPages",更新 「 3.0.0.0"版本從版本"2.0.0.0"這些項目If this section contains "System.Web.WebPages", update those elements from version "2.0.0.0" to version"3.0.0.0"

    <configuration>
      <configSections>
        <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
          <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        </sectionGroup>
      </configSections>
    
  3. 如果您移除Microsoft-Web-HelpersNuGet 封裝在先前步驟中,安裝Microsoft.AspNet.WebHelpers使用 pmc 依存在下列命令:If you removed the Microsoft-Web-Helpers NuGet package in a previous step, install Microsoft.AspNet.WebHelpers with the following command in the PMC:
    Install-Package -Id Microsoft.AspNet.WebHelpers

  4. 如果您的應用程式使用User.IsInRole()方法,將下列內容加入Web.config檔案。If your app uses the User.IsInRole() method, add the following to the Web.config file.

    <system.webServer>
        <modules>
          <remove name="RoleManager" />
        </modules>
      </system.webServer>
    

最後一個步驟Final Steps

建置及測試應用程式。Build and test the application.

從專案檔中移除 MVC 4 專案類型的 GUID。Remove the MVC 4 project type GUID from the project files.

  1. 在方案總管 中,以滑鼠右鍵按一下專案名稱,然後選取卸載專案In Solution Explorer, right-click the project name and then select Unload Project.
  2. 以滑鼠右鍵按一下專案,然後選取 [編輯 projectname.csproj]。Right-click the project and select Edit ProjectName.csproj.
  3. 找出ProjectTypeGuids項目,然後移除 MVC 4 專案 GUID, {E3E379DF-F4C6-4180-9B81-6769533ABE47}Locate the ProjectTypeGuids element and then remove the MVC 4 project GUID, {E3E379DF-F4C6-4180-9B81-6769533ABE47}.
  4. 儲存並關閉開啟的專案檔案。Save and close the open project file.
  5. 以滑鼠右鍵按一下專案,然後選取重新載入專案Right-click the project and select Reload Project.