8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ASP.NET MVC 4.0から5.2へのアップグレード

Last updated at Posted at 2014-07-08

Visual Studioのアップグレード

Visual Studioを2013 Update1以降にします。Updateが無い場合はRazorテンプレートの編集時に@modelなどが認識されなくなります。

諸事情によりVisual Studio 2012を使い続ける場合は以下のページを参考に「ASP.NET and Web Tools 2013.1 for Visual Studio 2012」をインストールします。

Visual Studio 2013 Update or higher. This update is needed for editing ASP.NET MVC 5.2 Razor Views.
- Announcing release of ASP.NET and Web Tools 2013.1 for Visual Studio 2012 - .NET Web Development and Tools Blog - Site Home - MSDN Blogs

対象フレームワークの変更

対象のフレームワークが.NET Framework 4.5になっていない場合はプロジェクトのプロパティから変更しておきます。

ASP.NET MVCのアップグレード

「NuGetパッケージの管理」からおもむろにASP.NET MVCをアップグレードします。

パッケージの再インストール

対象フレームワークのバージョンを変更すると、パッケージの再インストールを促される場合があります。メニューの「ツール→ライブラリ パッケージ マネージャー→パッケージ マネージャー コンソール」からパッケージマネージャーコンソールを開いてメッセージにあるコマンドを入力します。

Web.configの修正

/Web.configを修正します。

appSettings要素の中の「2.0.0.0」を「3.0.0.0」に変更。

Web.config
    <add key="webpages:Version" value="2.0.0.0" />

assemblyBinding要素の中の「2.0.0.0」を「3.0.0.0」に、「4.0.0.0」を「5.2.0.0」に変更。この部分はNuGetパッケージのアップデートの際に自動で書き換わっているはずです。

Web.config
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>

Views/Web.configに入っているASP.NET MVC関連のバージョンを変更します。Areaを作っている場合はその中のViews/Web.configも忘れずに変更します。
これを忘れると同じクラスの異なるバージョンを参照してしまって「System.Web.WebPages.Razor.Configuration.HostSectionをSystem.Web.WebPages.Razor.Configuration.HostSectionにキャストできない」というエラーが出る場合があります。

以下の「2.0.0.0」を「3.0.0.0」に変更。

Web.config
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

以下の「4.0.0.0」を「5.2.0.0」に変更。

Web.config
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

以下の「4.0.0.0」を「5.2.0.0」に変更。

Web.config
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>

以上で完了です。

他のパッケージも合わせてアップグレードした場合

Elmah.MVC 2.0.0から2.1.1へ

Elmah.Mvc.Bootstrap.Initialize() の呼び出しは要らなくなったようなので削除します。

MiniProfiler.MVC3をMiniProfiler.MVC4に

名前空間StackExchange.Profiling.MVCHelpers以下のクラスは StackExchange.Profiling.Mvc以下に移動しているのでusingを変更しておきます。

WebActivator

WebActivatorというパッケージは同名のパッケージのまま別なものになっているようなので気軽にアップデートしない方が無難です。

元々あったはずのクラスや名前空間が無いと言われた場合

何かのパッケージをアンインストールした際に依存するパッケージが自動でアンインストールされてしまう場合があるようです。あらかじめインストールされているパッケージをメモっておいて無かったら入れなおします。

参考

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?