Single using statement, multiple views MVC

I've written several extension methods (HTML helpers) and would like to use them in many views, this ...

@using My.Extensions.Namespace

@Html.MyExtension()

      

... obviously works. But that means including the operator using

in each separate view (there will be many).

If that's the only approach that's fine, but I'm wondering if there is a way to use this namespace "globally" without having to declare it on every separate view.

+3


source to share


1 answer


You can enable it via a file webconfig

inside a folder Views

:

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        ...
        <add namespace="My.Extensions.Namespace"/>
      </namespaces>
    </pages>
  </system.web.webPages.razor>

      



Every namespace that we need to reference in the Razor view must be declared either this way or in the view itself using an operator @using

.

+4


source







All Articles