Search code examples
stackexchangepre-compilation

How can I fix compilation for relative paths using StackExchange.Precompilation?


For https://github.com/StackExchange/StackExchange.Precompilation:

We have some shared views in separate library where we have installed StackExchange.Precompilation. We have to load those views along with the normal web project's views. We have the latest version of StackExchange.Precompilation installed from NuGet in both projects. I am doing the assembly loading like this:

// Register precompiled view engine
ViewEngines.Engines.Clear();

List<Assembly> viewAssemblies = new List<Assembly> { typeof(HomeController).Assembly };
viewAssemblies.AddRange(AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.ToLower().Contains(".web")));

Log.Debug().Message("Looking for views in: {0}", string.Join(", ", viewAssemblies.Select(a => a.FullName))).Write();

ViewEngines.Engines.Add(new PrecompiledViewEngine(viewAssemblies.ToArray()));

In the web project, we return views the normal way: return View("Index"); When using PrecompiledViewEngine we get an error when trying to render relative names like that:

The view 'Index' or its master was not found or no view engine supports the searched locations. 
The following locations were searched: 
~/util/Views/Example/Index.cshtml 
~/util/Views/Shared/Index.cshtml

util is the alias of the application in IIS. We don't have any areas registered.

When I copied the PrecompiledViewEngine class from GitHub - it worked! Am I missing something that will make the version distributed through NuGet work?


Solution

  • Copying the code over turned out not to work. I must have been trying something else at the time that made it work for that specific case.

    The problem is actually a bug in StackExchange.Precompilation. I've created an issue there: https://github.com/StackExchange/StackExchange.Precompilation/issues/12