When strongly typed views stop working after converting a MVC 1.0 project from Visual Studio 2008 to 2010

I just converted my blog from a Visual Studio 2008 project to a Visual Studio 2010 project using Visual Studio 2010’s conversion wizard. When you do that for a ASP.NET MVC 1.0 project the wizard automatically converts it to a MVC 2 project and changes all references to MVC 1.0 to MVC 2 in web.config.

As I’m using EPiServer CMS which at this point doesn’t support MVC 2 I changed those references back to MVC 1.0 in web.config and thought everything would work as before. But then I ran into a problem where the model object in strongly typed views wasn’t of the correct type, resulting in exceptions like this:

CS1061: 'object' does not contain a definition for 'Title' and no extension method 'Title' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

It took me a while to figure out what was going on since the error message was describing a symptom and not the problem. Since I had been using the standard template for ASP.NET MVC projects when I first set up the project it contains a separate web.config in the Views folder that I have never even bothered to open. Of course the wizard had changed there references from MVC 1.0 to MVC 2 there as well.

After changing

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

to

<pages validateRequest="false" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
    <controls>
        <add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" namespace="System.Web.Mvc" tagPrefix="mvc" />
    </controls>
</pages> 

Everything works fine again.

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Comments

  1. Matthew Fenelon's avatar

    Matthew Fenelon 1 years ago

    Thank you so much for publishing this page, I have been struggling with this issue for the last day!

  2. john.gates@cubic.com's avatar

    john.gates@cubic.com 1 years ago

    Many thanks, you're article put me on the right course- We had a related issue where we had upgraded an MVC S#arp project from VS 2008 to VS 2010 where the outer web config was set to MVC 2 while the inner view config was still at MVC version 1. The solution for me was to correct the View config to MVC 2.

  3. Stu's avatar

    Stu 11 months ago

    Great stuff, exactly what I was looking for, thanks.

  4. Paul's avatar

    Paul 8 months ago

    Many thanks for posting the fix.

    That additional web.config file was a bit sneaky!

  5. Xp's avatar

    Xp 8 months ago

    This is awesome.

    I havent done a conversion. and I dont have the second web.config in Views folder. But somehow in the actual web.config in root had all set to MVC version 1.0.0.0. So I changed to Version 2.0.0.0.
    Yes! Now my strongly typed views work!

    You saved my day.

    Thank you so much and long live.

  6. Shaun's avatar

    Shaun 4 months ago

    Saved me a ton of time, thanks for this post!

  7. deerchao's avatar

    deerchao 3 months ago

    Saved my sleep. Thanks!

Follow me on Twitter

  1. On the subject of things I wish I didn't have to know: http://t.co/Hdu9tCsw 19 hours ago
  2. @erickdennel thanks! 19 hours ago
  3. @erickdennel Yeah, I think I had to much work 19 hours ago
follow me

Latest comments

  1. Berra S wrote "Read your post at http://joelabrahamsson.com/entry/using-xfo..." on PageData objects not returned as typed when using Page Type Builder and FindPagesWithCriteria
  2. Linus wrote "1 up for behaviour being as close as expected as possible!" on A common problem with Page Type Builder and UniqueValuePerLanguage set to false
  3. Joel Abrahamsson wrote "Hi Hans, Could it be that you previously didn't have Page..." on Page Type Builder 2.0 released

About this site

This blog is built with EPiServer Community, EPiServer CMS, ASP.NET MVC and a bunch of other great products. The source code is available for download at the projects page, where you also can read more about this site and my other projects.

read more