System.Web.Optimization cannot be used in a razor view file in a Class Library project

1.3k Views Asked by At

I am working in a modularized asp.net mvc4 project. Some views are located in a Class Library project. I already set the web.config to enable the intelligence in razor views. The @model, @Html.* and others are all working well. But when I try to reference a js file in my view by using the @Scripts.Render(). It says cannot find "Scripts". I checked the web.config file. The System.Web.Optimization is in the // node. I also installed Microsoft.AspNet.Web.Optimization package by Nuget.

I tried to type the code in a .cs file to use the System.Web.Optimization members, it works. But I cannot do same thing in the .cshtml file. Even when I typing @using System.Web.Optimization, it prompt me the Optimization cannot be found.

I am not sure what else I can do to resolve this issue. Any clues are very appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

If you have a Web.config at the root of the class library, confirm it contains the following: <add namespace="System.Web.Optimization"/>

If you do not, trick Visual Studio into thinking that it is a web project and enable Intellisense and resolve your issue by adding a web.config file to the root of your class library project (the one that contains the Razor views) with the following contents (taken from this blog post) :

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.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.Optimization"/>
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <system.web>
    <compilation targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

The above is what worked for me when I faced this issue, but here are more links:

http://forums.asp.net/t/1812274.aspx/1

Getting System.Web.Optimization to work in a razor view within a class library