Maxime Rouiller
Maxime Rouiller

Reputation: 13709

Custom libraries with Razor in MVC 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?

Upvotes: 3

Views: 1829

Answers (2)

Greg B
Greg B

Reputation: 14898

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>

Upvotes: 5

Darin Dimitrov
Darin Dimitrov

Reputation: 1039408

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>

Upvotes: 10

Related Questions