Manually upgraded a MVC web app from 3 to 4, can't add API Controllers

asp.net, asp.net-mvc, asp.net-mvc-3, asp.net-web-api

Solution

Try removing these lines from your web.config

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

Problem

I upgraded my web app from MVC3 to 4 recently by creating a new web application in MVC4, and copying all the files over, and finally merging the web.config. All works well with the existing web app, however I've just noticed a very weird error today - if I try to create a new API controller with the default dummy implementation, and then browse to that api (e.g. /api/events), I get a `Could not load type` error. The weird thing is if I rebuild the app, and hit the URL again, it seems to be a different assembly it's complaining about, mainly it alternates between these 2: ``` Could not load type 'System.Web.Razor.Parser.SyntaxTree.CodeSpan' from assembly 'System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. ``` and ``` Could not load file or assembly 'Microsoft.Scripting, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1' or one of its dependencies. The system cannot find the file specified. ``` My web.config contains the following assembly binding information: ``` <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <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.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="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> </dependentAssembly> <dependentAssembly> <!--<assemblyIdentity name="FluentValidation" publicKeyToken="a82054b837897c66" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-3.2.0.0" newVersion="3.2.0.0" />--> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" /> <bindingRedirect oldVersion="0.0.0.0-4.0.8.0" newVersion="4.0.8.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> </assemblyBinding> </runtime> ``` And my packages.config looks like it has all the right bits in it: ``` <package id="AspNetMvc" version="4.0.20126.16343" /> <package id="AspNetRazor.Core" version="2.0.20126.16343" /> <package id="AspNetWebApi" version="4.0.20126.16343" /> <package id="AspNetWebApi.Core" version="4.0.20126.16343" /> <package id="AspNetWebPages.Core" version="2.0.20126.16343" /> <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" /> <package id="Microsoft.Web.Optimization" version="1.0.0-beta" /> <package id="System.Net.Http" version="2.0.20126.16343" /> <package id="System.Net.Http.Formatting" version="4.0.20126.16343" /> <package id="System.Web.Http.Common" version="4.0.20126.16343" /> <package id="System.Web.Providers" version="1.1" /> <package id="System.Web.Providers.Core" version="1.0" /> ``` I've even tried copying over my web.config with a "vanilla" one from a new mvc4 web app, which still gives me the same error (could not load file or assembly Microsoft.Scripting)

Original source