开发者

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 usi开发者_如何学运维ng 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?


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>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜