<p>I'm trying to create a webapplication where I want to be able to plug-in separate assemblies. I'm using MVC preview 4 combined with Unity for dependency injection, which I use to create the controllers from my plugin assemblies. I'm using WebForms (default aspx) as my view engine.</p>

<p>If I want to use a view, I'm stuck on the ones that are defined in the core project, because of the dynamic compiling of the ASPX part. I'm looking for a proper way to enclose ASPX files in a different assembly, without having to go through the whole deployment step. Am I missing something obvious? Or should I resort to creating my views programmatically?</p>

<hr>

<p>Update: I changed the accepted answer. Even though Dale's answer is very thorough, I went for the solution with a different virtual path provider. It works like a charm, and takes only about 20 lines in code altogether I think.</p>

[{'answer_id': 35250, 'author': 'Joseph Kingry', 'author_id': 3046, 'author_profile': 'https://Stackoverflow.com/users/3046', 'pm_score': 5, 'selected': True, 'text': '<p>Essentially this is the same issue as people had with WebForms and trying to compile their UserControl ASCX files into a DLL. I found this <a href="http://www.codeproject.com/KB/aspnet/ASP2UserControlLibrary.aspx" rel="noreferrer">http://www.codeproject.com/KB/aspnet/ASP2UserControlLibrary.aspx</a> that might work for you too.</p>\n'}, {'answer_id': 1514764, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<pre><code>protected void Application_Start()\n{\n    WebFormViewEngine engine = new WebFormViewEngine();\n\n    engine.ViewLocationFormats = new[] { "~/bin/Views/{1}/{0}.aspx", "~/Views/Shared/{0}.aspx" };\n    engine.PartialViewLocationFormats = engine.ViewLocationFormats;\n\n    ViewEngines.Engines.Clear();\n    ViewEngines.Engines.Add(engine);\n\n    RegisterRoutes(RouteTable.Routes);\n}\n</code></pre>\n\n<p>Set the \'Copy to output\' property of your view to \'Copy always\'</p>\n'}, {'answer_id': 2489049, 'author': 'Erik van Brakel', 'author_id': 909, 'author_profile': 'https://Stackoverflow.com/users/909', 'pm_score': 2, 'selected': False, 'text': "<p>An addition to all you who are still looking for the holy grail: I've come a bit closer to finding it, if you're not too attached to the webforms viewengine.</p>\n\n<p>I've recently tried out the Spark viewengine. Other than being totally awesome and I wouldn't go back to webforms even if I was threathened, it also provides some very nice hooks for modularity of an application. The example in their docs is using Windsor as an IoC container, but I can't imagine it to be a lot harder if you want to take another approach.</p>\n"}, {'answer_id': 4573845, 'author': 'Tom Clarkson', 'author_id': 101970, 'author_profile': 'https://Stackoverflow.com/users/101970', 'pm_score': 5, 'selected': False, 'text': '<p>It took me way too long to get this working properly from the various partial samples, so here\'s the full code needed to get views from a Views folder in a shared library structured the same as a regular Views folder but with everything set to build as embedded resources. It will only use the embedded file if the usual file does not exist.</p>\n\n<p>The first line of Application_Start:</p>\n\n<pre><code>HostingEnvironment.RegisterVirtualPathProvider(new EmbeddedViewPathProvider());\n</code></pre>\n\n<p>The VirtualPathProvider</p>\n\n<pre><code>   public class EmbeddedVirtualFile : VirtualFile\n{\n    public EmbeddedVirtualFile(string virtualPath)\n        : base(virtualPath)\n    {\n    }\n\n    internal static string GetResourceName(string virtualPath)\n    {\n        if (!virtualPath.Contains("/Views/"))\n        {\n            return null;\n        }\n\n\n\n        var resourcename = virtualPath\n            .Substring(virtualPath.IndexOf("Views/"))\n            .Replace("Views/", "OrangeGuava.Common.Views.")\n            .Replace("/", ".");\n\n        return resourcename;\n\n    }\n\n\n    public override Stream Open()\n    {\n        Assembly assembly = Assembly.GetExecutingAssembly();\n\n\n        var resourcename = GetResourceName(this.VirtualPath);\n        return assembly.GetManifestResourceStream(resourcename);\n    }\n\n\n\n\n}\n\npublic class EmbeddedViewPathProvider : VirtualPathProvider\n{\n\n\n    private bool ResourceFileExists(string virtualPath)\n    {\n\n        Assembly assembly = Assembly.GetExecutingAssembly();\n\n\n        var resourcename = EmbeddedVirtualFile.GetResourceName(virtualPath);\n        var result = resourcename != null &amp;&amp; assembly.GetManifestResourceNames().Contains(resourcename);\n        return result;\n    }\n\n    public override bool FileExists(string virtualPath)\n    {\n        return base.FileExists(virtualPath) || ResourceFileExists(virtualPath);\n    }\n\n\n    public override VirtualFile GetFile(string virtualPath)\n    {\n\n        if (!base.FileExists(virtualPath))\n        {\n            return new EmbeddedVirtualFile(virtualPath);\n        }\n        else\n        {\n            return base.GetFile(virtualPath);\n        }\n\n    }\n\n}\n</code></pre>\n\n<p>The final step to get it working is that the root Web.Config must contain the right settings to parse strongly typed MVC views, as the one in the views folder won\'t be used:</p>\n\n<pre><code>&lt;pages\n    validateRequest="false"\n    pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"\n    pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"\n    userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"&gt;\n  &lt;controls&gt;\n    &lt;add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" /&gt;\n  &lt;/controls&gt;\n&lt;/pages&gt;\n</code></pre>\n\n<p>A couple of additional steps are required to get it working with Mono. First, you need to implement GetDirectory, since all files in the views folder get loaded when the app starts rather than as needed:</p>\n\n<pre><code>public override VirtualDirectory GetDirectory(string virtualDir)\n    {\n        Log.LogInfo("GetDirectory - " + virtualDir);\n        var b = base.GetDirectory(virtualDir);\n        return new EmbeddedVirtualDirectory(virtualDir, b);\n    }\n\npublic class EmbeddedVirtualDirectory : VirtualDirectory\n{\n    private VirtualDirectory FileDir { get; set; } \n\n    public EmbeddedVirtualDirectory(string virtualPath, VirtualDirectory filedir)\n        : base(virtualPath)\n    {\n        FileDir = filedir;\n    }\n\n    public override System.Collections.IEnumerable Children\n    {\n        get { return FileDir.Children; }\n    }\n\n    public override System.Collections.IEnumerable Directories\n    {\n        get { return FileDir.Directories; }\n    }\n\n    public override System.Collections.IEnumerable Files\n    {\n        get {\n\n            if (!VirtualPath.Contains("/Views/") || VirtualPath.EndsWith("/Views/"))\n            {\n                return FileDir.Files;\n            }\n\n            var fl = new List&lt;VirtualFile&gt;();\n\n            foreach (VirtualFile f in FileDir.Files)\n            {\n                fl.Add(f);\n            }\n\n\n            var resourcename = VirtualPath.Substring(VirtualPath.IndexOf("Views/"))\n.Replace("Views/", "OrangeGuava.Common.Views.")\n.Replace("/", ".");\n\n            Assembly assembly = Assembly.GetExecutingAssembly();\n\n            var rfl = assembly.GetManifestResourceNames()\n                .Where(s =&gt; s.StartsWith(resourcename))\n                .Select(s =&gt; VirtualPath + s.Replace(resourcename, ""))\n                .Select(s =&gt; new EmbeddedVirtualFile(s));\n            fl.AddRange(rfl);\n\n            return fl;\n        }\n    }\n}\n</code></pre>\n\n<p>Finally, strongly typed views will almost but not quite work perfectly. Model will be treated as an untyped object, so to get strong typing back you need to start your shared views with something like</p>\n\n<pre><code>&lt;% var Model2 = Model as IEnumerable&lt;AppModel&gt;;  %&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/19746', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/909/']