Project Description
This project extends the capabilities of the PrecompiledMvcViewEngine (part of Razor Generator project). It supports precompiled Razor views in multiple assemblies.

Getting Started

Get it from NuGet .

Features

  • Controller action can return a compiled view from another assembly (which must be loaded and the view engine must know about it). Use the CompiledView attribute to decorate the action method like this
        [CompiledView(FromAssembly = "ClassLibrary1")]
        public ActionResult PluginView()
        {
            
            return View("~/MyView.cshtml"); //MyView is a compiled view from the ClassLibrary1 assembly
        }
  • If using a controller from another assembly (think plugin) the engine knows to return the view from that assembly.
For example, ClassLibrary1.dll (from the sample project) uses the default asp.net mvc application directory structure and it contains the Plugin1Controller class and the ~/Views/Plugin1/Index.cshtml view file. You can create it by copy/pasting the Controllers and Views directories from a standard asp.net mvc application.
  • Unobtrusive, it can be used with the default RazorViewEngine. You can have different action methods in the same controller which return
- A normal uncompiled view
- A compiled view from the same assembly
- A compiled view from another assembly

The source code contains a sample project which showcase different features.

Usage

You need the RazorGenerator tool VS extensions to compile the views.
This sample uses WebActivator to run the code before App_Start

[assembly: WebActivator.PreApplicationStartMethod(typeof(Activate), "Engine")]
namespace MvcApplication1
{
    
    
    public class Activate
    {
         public static void Engine()
         {
             var assemblies = new List<Assembly>();
             var current = Assembly.GetAssembly(typeof (MvcApplication));
             assemblies.Add(current);

             //get the dll containing compiled views            
             foreach(var file in Directory.EnumerateFiles(HostingEnvironment.MapPath(@"~\Compiled"),"*.dll"))
             {
                 try
                 {
                     var a = Assembly.Load(AssemblyName.GetAssemblyName(file));
                     assemblies.Add(a);
                     BuildManager.AddReferencedAssembly(a);
                 }
                 catch
                 {
                     
                 }
             }
  
             //the view engine will consider all the provided assemblies          
             var engine = new PrecompiledMvcEngine(assemblies);

             ViewEngines.Engines.Insert(0, engine);

             // StartPage lookups are done by WebPages. 
             VirtualPathFactoryManager.RegisterVirtualPathFactory(engine);  

            
         }

    }
}

Last edited Oct 28, 2011 at 6:32 AM by mike_sapiens, version 11