John Lam 1997 — 2026

Force .aspx pages to be compiled during daily build

I spent the better part of today trying to figure out how to compile .aspx pages without running them. I have a somewhat grotesque hack that uses PageParser.GetCompiledPageInstance(). It works, but only for .aspx pages. I’d love to be able to compile .ascx, .asmx, .ashx, and .asax pages as well.

I host the ASP.NET runtime in its own AppDomain via System.Web.Hosting.ApplicationHost.CreateApplicationHost(). One of the problems that I ran into was figuring out how to initialize HttpRuntime so that I can call PageParser.GetCompiledPageInstance(). Unfortunately, I can’t figure out how to initialize HttpRuntime without first running a page via HttpRuntime.ProcessRequest(). I’m not very happy with that as an option.

Any suggestions on how to get this done would be really helpful. My goal is to write a NAnt task that will let folks include .aspx files in their daily build process.

14 archived reader comments

Gerry Shaw (2003-08-02)

Create a new target that performs a “get” task on each page you want to precompile.

Richard Tallent (2003-08-02)

I think I can help with one of them: for ascx, just create a test aspx page that instantiates each of your ascx controls. Trickle-down compiling should take over from there.

But running them shouldn’t be that big a deal anyway… why not use WebClient and just request the pages? I might be missing the point.

John Lam (2003-08-02)

The problem with performing a “get” task is the risk of introducing side-effects in arbitrary .aspx pages. For example, you could begin a session when all you really wanted to do was check to see whether the page compiles.

So … what I’d like to do is just get the page to compile first, then run the appropriate tests if that succeeds.

Andy Hopper (2003-08-02)

Hmm. Is there a particular ason you’re not using codebehind? Using codebehind, you could use one assembly per class/page,one assembly containing the classes for all of the pages, or multiple assemblies with multiple classes. This would allow you to compile the page prior to testing, and if you took the route of one assembly per page you approximate the inline-code approach (it also creates a more modular Web app).

Andy Hopper (2003-08-02)

*sigh* ason = reason

John Lam (2003-08-02)

Hi Andy,

I prefer code-behind. But I want to build a general-purpose tool for folks who might have reasons to put code into .aspx files. For example, if you’re an ISV that’s building a vertical app that is customized in the field by your systems engineers, would you really want to let those guys take a build environment out into the field with them? It’s far better to let them customize the .aspx files in the field, and let the software engineers generate controlled builds of the core framework components.

Andy Hopper (2003-08-02)

Well, that’s a reasonable request. Let me make sure I understand what it is you’re trying to accomplish. It seems to me that your primary goal to be able to ensure the pages will compile (as opposed to precompiling for performance reasons), correct? If so, then I’d still recommend codebehind, but using the @Page directive’s relatively undocumented Src attribute. This approach will allow you to ship the code with the page so that it is field-editable, and also gives you something you can feed to a compiler. This approach also does not require VS.NET or the SDK to be installed. One note of caution, however: do not leave the assemblies that your task generates around anywhere where the ASP.NET runtime can see it (like the app’s bin folder) - the runtime WILL compile the code in the file specified by the Src attribute, and this would make the ASP.NET runtime very unhappy because it will find two classes with the same name (one in your precompiled assembly, another in the temporary assembly ASP.NET generates). The simplest approach would be to compile the code into a temporary assembly and delete the file when the task completes.

Duncan Smart (2003-08-04)

Our app requires forms authentication so coding up some class that authenticates using forms seemed too much hassle, so all we do is have a page which dynamically generates a bunch of IMG tags from the aspx files on the server, therefore this page then does a GET (via the IMGs) on all the aspx files in the app - therefore they all get compiled.

Note that even if you’re using code-behind everything in the ASPX file itself (even the static HTML) gets compiled to a DLL (in Temporary ASP.NET Files) so what you’re doing is worthwhile even for code-behind as there’s still a considerable lag on the very first request to an ASPX page.

John Lam (2003-08-04)

Andy, You’re right - I just want to make sure that the page compiles, I’m not pre-compiling for performance reasons. I feel that it is very useful to know that ALL of the code in your application compiles when you run your daily builds (that it all works as well is an exercise left to the reader). Unfortunately, I can’t tell folks “don’t use ” tags because they’re bad for you :)

Yves Reynhout (2003-08-06)

Maybe Cassini src can help: http://www.asp.net/Projects/Cassini/Download/

Andrew Stopford (2003-08-08)

Take a look at the following

http://hyperthink.net/blog/permalink.aspx?guid=271632d2-07e3-41af-9e58-9a7e25348b8c

Kirk Jackson (2003-08-08)

This would be useful to do. When I was / am a Java developer, I find it useful to script jspc to compile the JSP pages.

I’d be interested to know what happens when you send a HEAD request, rather than a GET. I wonder if the same session / security code is run (and if the page would have the same side-effects, such as page_load being run).

I’d suspect that it is, as the content probably needs to be regenerated in order to send the size back in the response — I just can’t test it out at the moment.

I wonder if it’d be possible to send through a different verb instead of GET, POST or HEAD, and then ignore the request somehow if that verb is used. This would probably require some webserver config, like the DEBUG one requires.

Kirk

Alexander Blood (2003-08-20)

You could just create an HttpHandler for all the pages or one for each page. Then compile dll(s) and then run Ngen on that dll to speed up the application. ASP.NET does a lot of code generation given .asp# files that could be done a quicker way with less effort on your side. It still is a aspx file name that would be requested it just would never have to exist at all, the handler would take over and do what ever you wanted it to do for you.

Just an idea.

Kent (2004-02-09)

How is it?