biox
biox

Reputation: 1566

How to avoid using "using System.Web.Optimization" in layout page

I started empty project so there was no @Styles.Render, I had to download Web.Optimization from nuget packages and create BundleConfig.cs class in App_Start folder. Then do avoid writing @using System.Web.Optimization in my _Layout.cshtml I added namespace in the Web.config in the Views folder.

<pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
        ...
        <add namespace="System.Web.Optimization" />
        ...
    </namespaces>
</pages>

But this not solved my problem, I still need to write @using System.Web.Optimization. What I doing wrong?

Upvotes: 3

Views: 1687

Answers (1)

Matthieu
Matthieu

Reputation: 4620

As you described, the only way is to add the namespace in the following part of your web.config in the views folder (or the appropriate parent folder if your views are in a different area) :

  <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" />
        ...
     </namespaces>
  </pages>

just add the following line :

  <add namespace="System.Web.Optimization" />

You will need to rebuild the solution to make it effective.

Depending on the version of ASP.NET and Visual Studio (there are some incompatibilities between versions), you may need to also:

  • close and reopen your layout file (.cshtml).
  • restart Visual Studio if the previous item didn't work.

Upvotes: 4

Related Questions