LoginSignup
0
0

More than 3 years have passed since last update.

MVC3からMVC5に移行するときにしたこと

Last updated at Posted at 2020-06-29

Web.config内のMVCのバージョン修正

プロジェクトフォルダ直下のWeb.config

  • 修正前
<configuration>
  <appSettings>
    <add key="webpages:Version" value="1.0.0.0"/>
  • 修正後
<configuration>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>

プロジェクトフォルダ/View/Web.config

  • 修正前
<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXX">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXX" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXX" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=XXX" />
  • 修正後

Version= の値を修正

<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=XXX">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=XXX" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=XXX" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=XXX" />

Razor構文チェックが厳しくなった

コードブロック内に不要な@がある場合、MVC3ではエラーにならなかったが、MVC5ではエラーになるようになった。

参考: https://docs.microsoft.com/ja-jp/aspnet/core/mvc/views/razor?view=aspnetcore-3.1
「Razor ファイルに余分な @ 文字があると、ブロックの後続のステートメントでコンパイラ エラーが発生することがあります。」

例1

  • 修正前
@{
    ViewBag.Menu = "Main Menu";
    ViewBag.Title = @Html.Resource("title");
}
  • 修正後
@{
    ViewBag.Menu = "Main Menu";
    ViewBag.Title = Html.Resource("title");
}

例2

  • 修正前
@if (!string.IsNullOrEmpty(Model.Hoge))
{
    @string.Format("NAME:{0}", Model.Hoge)
}
  • 修正後
@if (!string.IsNullOrEmpty(Model.Hoge))
{
    <text>@string.Format("NAME:{0}", Model.Hoge)</text>
}

input type="hidden"のvalueにbool値を設定するときの扱いが変わった

HTML5の仕様に準拠した形になったらしい。
参考: https://stackoverflow.com/questions/28849729/asp-net-mvc-5-renders-different-bool-value-for-hidden-input

MVC3の場合

valueにbool値を設定すると、true/falseに変換される

<input type="hidden" id="isHoge" value="@Model.IsHoge" />

<input type="hidden" id="isHoge" value="true" />

MVC5(MVC4以降)の場合

  • @Model.IsHogeがtrueの場合
<input type="hidden" id="isHoge" value="value" />
  • @Model.IsHogeがfalseの場合
<input type="hidden" id="isHoge" />
  • 修正後
<input type="hidden" id="isHoge" value="@Model.IsHoge.ToString()" />
0
0
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
0
0