Razor Class Library (RCL) _ViewImports ignored when site runs

Viewed 492

I've created a Razor Class Library (RCL) to create a 'common UI' for several sites following the pattern from this article. But I'm having a problem with the _ViewImports.cshtml.

In my RCL, I have a _TemplateLayout.cshtml. It uses some classes from a library named BTR.Camelot.Infrastructure. So in the _ViewImports.cshtml, I have:

@using BTR.Camelot.Infrastructure.Serialization

So that the RCL will compile. Without that, it does not compile.

When I run my project that references the RCL and uses its template, I get the following error:

The name 'JavascriptSerializer' does not exist in the current context Sys.CultureInfo.CurrentCulture = Sys.CultureInfo._parse(@Html.Raw( JavascriptSerializer.MicrosoftAjaxCultureInfo ));

JavascriptSerializer is in my BTR.Camelot.Infrastructure.Serialization namespace. My guess is that:

  1. The _ViewImports.cshtml is used in the RCL to make sure it can compile.
  2. The _ViewImports.cshtml from the site takes precedence over the _ViewImports.cshtml from the RCL and the RCL version is completely ignored.

Is there a way to make the _ViewImports.cshtml files 'merge'? This question seems to acknowledge that this problem exists but just accepted that you should remove the sites _ViewImports.cshtml. I'm hoping there is a work around.

My Project Structure

RootFolder
+-- SiteTemplates
|   +-- Evolution
|   |   +-- Pages
|   |   |   +-- _ViewImports.cshtml
|   |   +-- Evolution.csproj
|
+-- Websites
|   +-- Personal
|   |   +-- RTC
|   |   |   +-- Pages
|   |   |   |   +-- Shared
|   |   |   |   |   +-- _TemplateLayout.cshtml
|   |   |   |   +-- _ViewImports.cshtml
|   |   |   +-- RTC.csproj
|
+-- Infrastructure
|   +-- Infrastructure.csproj

RTC (main site) references Infrastructure.csproj and Evolution.csproj (RCL).

Evolution.csproj references Infrastructure.csproj.

Evolution/Pages/_ViewImports.cshtml

@using BTR.Camelot.Infrastructure.Configuration
@using BTR.Camelot.Infrastructure.Serialization
@using BTR.Camelot.SiteTemplates.Evolution.Resources

@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Options

@namespace BTR.Camelot.SiteTemplates.Evolution.Pages

RTC/Pages/_ViewImports.cshtml

@namespace BTR.Camelot.Websites.Personal.RTC.Pages

@using BTR.Camelot.Websites.Personal.RTC.Models
@using BTR.Camelot.Infrastructure.Configuration
@using BTR.Camelot.Core.Extensions.LINQ
@* Not using/needing Serialization here, but RCL Layout file does.  See comment in RCL _ViewImports.cshtml *@
@using BTR.Camelot.Infrastructure.Serialization

@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Options

And if I remove or comment out @using BTR.Camelot.Infrastructure.Serialization in the RTC project, then I receive the exception.

0 Answers
Related