Include (@using) a namespace in every page in an MVC site

Friday, 9 December 2011 14:35 by Chad

If you create some Url helper extensions and you'd like them to be immediately available on every page without needing to put @using at the top of every cshtml file you can:

Add an entry to the namespaces element in the web.config inside the Views folder:

/<project>/Views/Web.config

<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" />
			<add namespace="Company.Product.Site.Extensions"/>
		</namespaces>
	</pages>
</system.web.webPages.razor>