Custom libraries with Razor in MVC 3

So, I am developing an internal 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 -> section of the namespace and ... no. Does not work.

I tried recompiling, etc., but Razor didn't like it at all. This is not an intellisense problem ... the site cannot work if I use my specific namespace.

The only way I worked was as follows:

@using Sample.Helpers

I do not want to use it on pages. I want to be able to deploy it in many projects and add it to web.config is definitely the way to go.

Has anyone encountered this issue?

+3
source share
2 answers

~/Views/web.config, Razor :

<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>
+10

Razor

 <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>
+5

Source: https://habr.com/ru/post/1785934/


All Articles