upgrading from MVC4 to MVC5

37,664

Solution 1

Found out what need to be done in Views\Web.config. Just copy & paste these sections over what you have:

<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>

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

<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>

And that was it. Job done! :D

Solution 2

i suggest to see this from http://asp.net website

http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2

Solution 3

have you just updated the project references? Did you also check the web.config? There are also those libraries referenced and usually with specific version numbers of the library itself.

in the web.config you can find the referenced libraries for instance in:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="System.Web.Helpers" 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.WebPages" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

or

<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>

or

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
        </namespaces>
    </pages>
</system.web.webPages.razor>
Share:
37,664
Jack M
Author by

Jack M

Updated on September 02, 2020

Comments

  • Jack M
    Jack M over 3 years

    I have made that dreadful error of upgrading from MVC4 to MVC5 pre-release by updating the razor, and mvc webpage in my references I have System.Web.Mvc, System.Web.Webpages, System.Web.Webpages.Razor and System.Web.Razor as version v4.0.30319, when I run my application I get

    [A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to 
    [B]System.Web.WebPages.Razor.Configuration.HostSection.
    
    Type A originates from 
    'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location
    'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'.
    
    Type B originates from
    'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, 
    PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 
    'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\membership\c70f06fe\9163b1ca\assembly\dl3\291c956e\73c25daa_cf74ce01\System.Web.WebPages.Razor.dll'.
    

    is this the same as http://www.asp.net/whitepapers/mvc4-release-notes

    Thanks

    Adding a stacktrace:

    [InvalidCastException: [A]System.Web.WebPages.Razor.Configuration.HostSection cannot be      cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. 
    Type A originates from 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 
     'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location
    
    'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\c70f06fe\9163b1ca\assembly\dl3\291c956e\73c25daa_cf74ce01\System.Web.WebPages.Razor.dll'.]
    System.Web.WebPages.Razor.WebRazorHostFactory.CreateHostFromConfig(String virtualPath, String physicalPath) +193
    System.Web.WebPages.Razor.RazorBuildProvider.GetHostFromConfig() +51
    System.Web.WebPages.Razor.RazorBuildProvider.CreateHost() +24
    System.Web.WebPages.Razor.RazorBuildProvider.get_Host() +34
    System.Web.WebPages.Razor.RazorBuildProvider.EnsureGeneratedCode() +85
    System.Web.WebPages.Razor.RazorBuildProvider.get_CodeCompilerType() +34
    System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider) +189
    System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders() +265
    System.Web.Compilation.BuildProvidersCompiler.PerformBuild() +21
    System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) +580
    System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +571
    System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +203
    System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) +249
    System.Web.Compilation.BuildManager.GetCompiledType(VirtualPath virtualPath) +17
    System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +90
    System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +380
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +109
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +890
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +97
    System.Web.Mvc.Async.<>c__DisplayClass1e.<BeginInvokeAction>b__1b(IAsyncResult asyncResult) +241
    System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +19
    System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +51
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
    
  • Captain Kenpachi
    Captain Kenpachi almost 11 years
    I was pretty much going to say the exact thing. I could maybe just add that you can put a version redirect in your web.config file too. Also, remember that your Views folder also contains a web.config file.
  • Jack M
    Jack M almost 11 years
    @JuannStrauss: I used nuget and it does that for you
  • Leniel Maccaferri
    Leniel Maccaferri almost 11 years
    Nice one BRO... helped me a lot! :) Had to change it in 3 Web.config files (inside Views folders) - the main one and other 2 inside Areas. Phew! They could've updated it automagically using the NuGet package through a PowerShell script or something...
  • Gerald Davis
    Gerald Davis almost 11 years
    @JackM What nuget package is available for MVC5. Can you provide the specific nuget commands you used?
  • Gerald Davis
    Gerald Davis almost 11 years
    Well I guess I googled badly before asking. The command is "Install-Package Microsoft.AspNet.Mvc -Pre" nuget.org/packages/Microsoft.AspNet.Mvc/5.0.0-beta2
  • Blaise
    Blaise over 10 years
    Is the Web.Config ones in Views Folder only? Am I supposed to leave the system level Web.Config untouched? Also, I assume the <host> and <pages> tags are under <system.web.webPages.razor>, isn't it?
  • Jack M
    Jack M over 10 years
    @Blaise: You are rightit is the views web.config. and the reason I had the issue was that I updated to the pre-release with the release of Visual Studio 2013 I guess this issue will be resolved but If you had a VS 2012 project ad wanted to port it to VS2013 then it is worth having a look. Host is outside the <system.web.webPages.razor>
  • bob.mazzo
    bob.mazzo over 9 years
    trying so hard to upgrade a asp.net test project from mvc4 to mvc5
  • theJerm
    theJerm over 9 years
    This solved my issue - I had to update my root web.config and the web.config in my Views folder.
  • JustJohn
    JustJohn over 8 years
    Oh yes. I know we aren't supposed to post "atta boy" comments but I journeyed far and wide all day after deciding to switch from Linq to SQL to Entity Framework and also to get rid of all Authorization gobbly gook by MS. It just shouldn't be this tightly coupled and tangled.
  • Craig Gjerdingen
    Craig Gjerdingen over 7 years
    You'll also likely find this link useful for MVC migrations docs.nuget.org/consume/package-restore/…