ApplicationParts are a feature in ASP.Net WebPages that allow you to register and execute precompiled webpages as regular pages. The _Admin module and the Razor debugger (http://nuget.org/List/Packages/RazorDebugger) are examples of precompiled web pages. To generate precompiled websites specify the host property as WebPages.

@* Generator: WebPage *@

Once compiled, notice that the generated class has a PageVirtualPath attribute. The value of this attribute is derived from the project-relative path of the file. This determines the path to this page in a application where it is registered.

[System.Web.WebPages.PageVirtualPathAttribute("~/WebPagesSample.cshtml")]
public class WebPagesSample_cshtml : System.Web.WebPages.WebPage

Once you’ve precompiled your pages, you need to write bootstrapping code that registers the assembly as an ApplicationPart. One way to go about this is to write a PreApplicationStartMethod.

// AssemblyInfo.cs
[assembly: PreApplicationStartMethod(typeof(PrecompiledTest.PreApplicationStartCode), "Start")]

// PreApplicationStartCode.cs
using System.Web;
namespace PrecompiledTest {
    public static class PreApplicationStartCode() {
        private static bool _startCalled;

        public static void Start() {
             public static void Start() {
             if (_startMethodExecuted == true) {
                 return;
              }
              string root = "~/MyModuleRoot";
              ApplicationPart.Register(new ApplicationPart(typeof(PreApplicationStartCode).Assembly, root));
        }
    }
}

Pages from your precompiled assembly would now be available under the root path that you specified e.g. ~/MyModuleRoot/WebPagesSample

Last edited Apr 28, 2011 at 2:06 AM by pranavkm, version 2

Comments

CraigSmitham Dec 28, 2011 at 7:28 PM 
There are some problems with the above code, here is a better version:

// AssemblyInfo.cs
using System.Web;
[assembly: PreApplicationStartMethod(typeof(PrecompiledTest.PreApplicationStartCode), "Start")]

// PreApplicationStartCode.cs
using System.Web;
using System.Web.WebPages;
namespace PrecompiledTest {
public static class PreApplicationStartCode
{
private static bool _startCalled;


public static void Start()
{
if (_startCalled == true)
{
return;
}
string root = "~/MyModuleRoot";
ApplicationPart.Register(new ApplicationPart(typeof(PreApplicationStartCode).Assembly, root));

_startCalled = true;
}
}
}