Fix For: does not exist in the current context

Feb 18, 2013 at 3:39 AM
If you're building a library with your pre-compiled views and you begin getting the dreaded red squigglies and the error {something} does not exist in the current context. Check if you deleted the web.config file (since you may not need it in the project). Replacing it may fix your issue.

This has caused me problems twice and I didn't even know it. I even restarted an entire project to fix it - and I thought it was something in the project file or other setting.
Feb 18, 2013 at 3:48 AM
Yes, and you mean specifically the Web.config in the Views folder, right?
Feb 18, 2013 at 4:04 AM
Yes, but that one I didn't delete. But it is needed.

Perhaps define my scenario first: My project is set up as an MVC4 project encompassing a "control panel." I drop its DLL into another MVC project and I have a control panel as part of any MVC project.

So the CP needs the following in the root web.config:
        <add assembly="System.Web.Abstractions, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add assembly="System.Web.Routing, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add assembly="System.Data.Linq, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
And I found that the Views web.config needs the following:
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
There may be a few more requirements, but I didn't delete node-by-node for testing (glad I got it working again); even though Layout is not recognized, even though it builds and works.
Feb 18, 2013 at 4:18 AM
In my experience, the simplest thing to do is to use an actual MVC project for the library, and then delete the things that are clearly not needed there, like controllers, and global.asax. It's easier than to start from a library project and try to add all the things that make intellisense happy,
Feb 18, 2013 at 4:20 AM
Definitely! That's what I did - but I thought I didn't need the web.config it the root. Oops :-/