如何將 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. 從 Web API 升級至 Web API 2 中,在 global.asax,變更: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 適用於只有第 3 Razor 和不 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
      此套件包含 bug 已修正在 MVC 5 的 MVC 4 中因應措施。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 」 的項目,變更 「 version=5.0.0.0"從"4.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>區段,並更新到 3.0.0.0 webpages:version 從 2.0.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 [Views] 資料夾下的檔案Update the web.config files under the Views folder

如果您的應用程式使用的區域,您也必須更新每個web.config中的檔案檢視的每個 Areas 資料夾的子資料夾。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. 更新版本 「 version=5.0.0.0"版本"4.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"版 「 System.Web.WebPages.Razor 」 到 「 3.0.0.0"版的所有項目。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.