Minimal Configuration Required for Razor Intellisense in ASP.NET MVC 3 RTM

   Submit to Reddit      
  

Recently I have been creating some custom ASP.NET MVC 3 Helpers and have been working with some customised Visual Studio 2010 Web Projects (we effectively separate our Web Areas into individual Web Projects).

When working in these customised Web Projects for Web Areas, I have faced some issues with the Razor Intellisense. As it turns out, the issues were actually due to a lack of understanding of Razor's requirements for populating its Intellisense.

And so, here is the absolute minimal configuration needed to get Intellisense working properly in Razor for an ASP.NET MVC 3 Web Project.

(1) A Visual Studio Web Project (sorry, I have not tried Class Library Projects)

(2) A web.config file in the root of the project, with the following contents:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
      <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

This is a cut-down version of the file that is created when you create a brand new, empty MVC 3 Web Project in Visual Studio.

Razor looks to this file in order to determine which assemblies from the GAC to load into its Intellisense. The assemblies listed above are the ASP.NET MVC 3 assemblies that contain the base class for a Razor view (System.Web.Mvc.WebViewPage) and the extension methods for all the standard MVC Helpers - such as HtmlHelper which is accessed through the @Html syntax, etc.).

If you had your own MVC Helpers that were strong named and deployed to the GAC, you could add them to the assemblies element.

All assemblies that are in the Web Project's private Bin folder are automatically loaded and made available in the Intellisense.

(3) A web.config file in the Views folder, with the following contents:

<?xml version="1.0"?>
<configuration>

  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

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

  <system.web>
    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>

    <!--
        Enabling request validation in view pages would cause validation to occur
        after the input has already been processed by the controller. By default
        MVC performs request validation before a controller processes the input.
        To change this behavior apply the ValidateInputAttribute to a
        controller or action.
    -->
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />

    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>

</configuration>

This is exactly the same file that is created when you create a brand new, empty MVC 3 Web Project in Visual Studio.

This web.config file importantly:

(a) Declares the default class that a Razor View/page inherits from (note: System.Web.Mvc.WebViewPage contains the Html and Ajax properties that are referenced by @Html and @Ajax respectively). The class that a specific Razor View inherits from can be overridden in the Razor syntax with the keyword @inherits.

(b) Declares the namespaces that are automatically imported - instead of having to use the @using Razor syntax. This is particularly important because it is the mechanism through which the MVC Helper extension methods are made available.

And that is all you need to get Razor Intellisense working!



comments powered by Disqus