ASP.NET MVC3 Razor - lost intellisense when placing view in alternate location?
Asked Answered
M

1

7

VS2010 Ultimate, ASP.NET MVC 3 w/Razor.

I've created a custom view engine in my MVC3 app that allows nested areas, like so

~/areas/admin
    /marketing
        /views
            index       
        /controllers
            marketingController
        /email
            /views
                index
                ...
            /controllers
                emailController
        /templates
            /views
                index
                edit
                ...                  
            /controllers
                templatesControler

etc.

This all works great, except for I seem to have lost intellisense in views that aren't in the standard ~/areas/area_name/views/myview.cshtml location.

Any suggestions?

Update

Just on a lark, I added an @inherits statement

@inherits System.Web.Mvc.WebViewPage<Namespace.Models.Class>

and intellisense started working. I then deleted the statement, and it continues to work.

Is there some setting in the project file or something that tells Visual Studio what sort of intellisense to apply to an open file, other than the file extension? (If the extension was all that was used, I'd expect it to be a whole lot more consistent).

Update 2

While adding a web.config to each of my view folders fixes the problem quite nicely, putting the razor config in the root web.config doesn't.

After adding the required system.web.webPages.razor section to ~/web.config,

  1. I add a Razor .cshtml view file to one of my nested view folders.
  2. Intellisense works.
  3. I rename the file (keeping the .cshtml extension)
  4. Intellesense and syntax highlighting stop working.
  5. I close the renamed file and reopen it - everything starts working again.
  6. Or, instead of closing and reopening the file, I rename it back to its original name, it starts working again.

This feels like a VS bug - renaming a file (but keeping the proper extension) shouldn't, as far as I can tell, cause this sort of behavior. I'll continue putting individual web.configs in each of my view folders since it corrects the problem, but it's annoying to have to clutter up the solution when I'd expect these settings to propagate through the solution tree as other web.config settings do.

It's also worth noting that, no matter where the Razor config is located, the site continues to function correctly whether or not intellesense is working.

Solved

I was informed (incredibly quickly) by the Powers That Be that this was a known issue that is corrected in MVC3 RTM and VS2010 SP1. The issue disappeared after updating my MVC3 installation and adding SP1.

Mahala answered 6/2, 2011 at 1:0 Comment(2)
You're right; that sounds like a VS bug. Email [email protected].Tour
Will do. Finally a reason to go see the Oracle.Mahala
T
7

The Views folder contains a Web.config file that sets default options for Razor views, including the default base type and namespaces, as well as a custom host that enables the @model directive.

Razor views in a different folder won't inherit this configuration.
You need to add this configuration to a Web.config in your folder:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.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>
Tour answered 6/2, 2011 at 3:44 Comment(4)
Excellent call, quick fix, though I don't really understand why these settings don't propagate from the root web.config. Any insight?Mahala
The root web config does not have these settings defined. But you could always add them yourself.Maineetloire
@David: They're only added to Views/Web.config to avoid breaking non-MVC pages in other folders.Tour
Okay, that makes sense. So, in theory, I could just put this section in the root config if the application is totally MVC.Mahala

© 2022 - 2024 — McMap. All rights reserved.