3

So I'm developing an in-house library for MVC 3 and I want to add it to my project.

I added it to my web.config. I added the assembly and added it to the pages -> namespaces section and... no. Doesn't work.

I tried recompiling, etc... but Razor doesn't like it at all. It's not an intellisense problem... the site can't run if I use my defined namespace.

The only way that I made it work was by using the following statements:

@using Sample.Helpers

I don't want to use it in the pages. I want to be able to deploy it to many projects and adding it to the web.config is definitely the way to go.

Anyone ran into this problem?

Factor Mystic
  • 26,279
  • 16
  • 79
  • 95
Maxime Rouiller
  • 13,614
  • 9
  • 57
  • 107
  • possible duplicate of [Razor syntax @using and namespace declarations fail](http://stackoverflow.com/questions/3875207/razor-syntax-using-and-namespace-declarations-fail) – marcind Jan 17 '11 at 18:00

2 Answers2

10

You need to add it in the ~/Views/web.config because Razor uses a different config section:

<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="YourNamespaceContainingTheHelperMethod" />
        </namespaces>
    </pages>
</system.web.webPages.razor>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • And that my friend solved my problem. I was adding it in the main web.config and it wasn't working at all. Now I need to figure out a way to add it to my NuGet package so that it works in both WebForm view engine and Razor but, main problem solved. :) Thanks man! – Maxime Rouiller Jan 17 '11 at 15:56
5

Razor uses a different config section

 <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>
    <pages pageBaseType="Foo.Bar">
      <namespaces>
        <add namespace="Foo.FooBar" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
Greg B
  • 14,597
  • 18
  • 87
  • 141