Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm building a modular MVC4 app, where each module (=area) is a class library. Models and controllers compile into the .dll, the views get copied into the appropriate folder. At runtime, everything works fine. At design time there is one annoying problem left: When editing a razor view within a class library, Visual Studio doesn't recognize the System.Web.Optimization Namespace.

The name "Styles" does not exist in the current context.
The name "Scripts" does not exist in the current context.

I tried adding the assembly to the system.web/compilation section in the root and the inner web.config:

<add assembly="System.Web.Optimization, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

I tried it without the specific version, too. Both methods didn't solve the problem, but triggered an asp.net runtime error (visible in the first line of the razor view):

Could not load file or assembly 'System.Web.Optimization, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

The assembly is referenced in the project and 'Copy Local' is set to 'True'. It's also added as a namespace in the razor configuration section.

I suspect it's a general problem i'll encounter with other assemblies in the future.

Edit: I did the general setup to get Intellisense going in a razor view within a class library. Everything works so far, except that VS2010 doesn't recognize the Optimization Namespace.

like image 323
Marcel Avatar asked Jul 21 '12 08:07

Marcel


People also ask

What is Razor class library?

Razor Class Library provides a way to package and distribute UI components to be referenced and reused within a host application. As this doc mentioned , Razor views, pages, controllers, page models, Razor components, View components, and data models can be built into a Razor class library (RCL).

Does .NET framework support Razor pages?

Introduced as part of ASP.NET Core, and now included in . NET 5, ASP.NET Razor Pages is a server-side, page-focused framework that enables building dynamic, data-driven web sites with clean separation of concerns.


3 Answers

Add 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>

This will trick Visual Studio into thinking that it is a web project and enable Intellisense.

like image 123
Darin Dimitrov Avatar answered Oct 15 '22 13:10

Darin Dimitrov


I had this error in pre builded view in asp mvc project for resolve i've installed Microsoft.Web.Optimization with nuget.

From the Tools menu, select Library Package Manager and then click Package Manager Console. Enter the following command to update Bundling and Minification:

Install-Package Microsoft.Web.Optimization -Pre

some additionals here http://forums.asp.net/t/1812274.aspx/1

like image 27
Vladimir Shmidt Avatar answered Oct 15 '22 11:10

Vladimir Shmidt


@Vladimir , I am using Visual Studio 2013, and had to slightly change your line to:

Install-Package Microsoft.AspNet.Web.Optimization

This fixed it for me.

like image 32
Saurabh Kumar Avatar answered Oct 15 '22 12:10

Saurabh Kumar