Add a namespace in all Razor views in a module

Topics: Writing modules
Feb 5, 2013 at 5:21 PM
I'm using Orchard 1.6 and would like to move the using directives from the Razor views to the Views folder web.config. This is a custom module. The syntax is described here - http://stackoverflow.com/questions/3239006/how-to-import-a-namespace-in-razor-view-page#6723046 - but I can't get it to work (the views can't find the classes).
Feb 5, 2013 at 5:53 PM
Are you doing this in the web.config of your module, or of the Orchard.Web project?
Feb 6, 2013 at 9:08 AM
There is a separate web.config in the Views folder of my module, which looks like the perfect place to do this.
Feb 6, 2013 at 12:09 PM
Are you saying that's where you tried it? Why not try Orchard.Web\web.config and see if it works?
Feb 6, 2013 at 12:22 PM
Because I want a specific namespace to be available in a single module views.
Feb 6, 2013 at 12:56 PM
Edited Feb 6, 2013 at 12:57 PM
Right, makes sense, my mistake.

I've done what you're asking by editing the web.config of my module in two places. In my case I was adding the System.Transactions namespace to be available in my module's Razor views:
<configuration>
<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <pages pageBaseType="Orchard.Mvc.ViewEngines.Razor.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="System.Linq" />
        <add namespace="System.Collections.Generic" />
        <add namespace="Orchard.Mvc.Html" />
        <add namespace="System.Transactions" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
</configuration>
<configuration>
  <system.web>
    <compilation 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.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <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" />
        <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
       <add assembly="System.Transactions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>
Feb 6, 2013 at 1:09 PM
Great, works for me.

Thanks Your Highness ;)