简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM