Adding Namespaces to MVC3 Views

Currently there are two ways to add an assemblies namespace to MVC3 views,either in the web.config or with @using.

When adding to the web.config, add the namespace to the web.config in the Veiws/Shared directory. Adding to the web.config in the project root doesn’t work, not sure if this is a bug or working as intended.

<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" />
    <add namespace="Raven.Web.Helpers" />
  </namespaces>
</pages>

My preference is to add a @using to the view, either on the master layout page a nested layout page or the view itself. It seems more natural to me to have it at the top of the page it is required as that is what I’m used to in code files.

One thought on “Adding Namespaces to MVC3 Views

  1. The problem with including it in the Views web.config file is that Visual Studio 2010 will not detect the imported namespace’s classes or class properties within cshtml views. The red Squiggly line will appear and no intellisense, but it does compile.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.