.NET Page Parsing The last couple days have led me on an interesting adventure: trying to get the .NET framework to load and parse ASPX pages dynamically. For ASCX controls, this is a simple process, as you just make a simple call...for pages, it is another story all together. As it turns out, there is a method called GetCompiledPageInstance, which will return an instance of the class for a given ASPX page. At first glance, this looked like the solution to my problems, but, alas, it does not actually do anything but create an uninitialized instance. The child controls are not there.
So, I did a little digging and found out that to initialize the control, you must process the request (of course, this is only because the methods I need to use are marked internal by the Microsofties). This is a little inefficient, but if it is the only way to do it, I can live with it. To process the request, you need a valid HttpContext and either an HttpRequest and an HttpResponse or an HttpWorkerRequest. Again, it turns out that you can't use the HttpRequest to do this, because it will always end up with an exception (in a property that gets called by the framework, an httpworkerrequest is not checked to see if it is null if you use the non-httpworkerrequest constructor, so an exception pops up...someone didn't read McConnell's book).
So, I dig some more. You can't directly instantiate HttpWorkerRequest, you need to create a SimpleWorkerRequest, so that seems easy enough. After finally getting the simple worker request to initialize properly, it turns out that you can't pass it in either, because SimpleWorkerRequest is apparently designed for creating your own mini-webservers, not loading ASPX files from ASPX files, and you end up with conflicts with the virtual directories somewhere inside the framework, and get presented with nice exception pages.
So, about a week or so after not receiving any useful feedback in the newsgroups (funny, I though MSDN subscribers were gaurenteed responses...), I continue my trek and decide to implement my own HttpWorkerRequest class. A pretty simple process, though painful for such a seemingly easy task. So, I pass this HttpWorkerRequest class into the HttpContext, and hold my fingers... After weeding out a few exceptions, everything is working. However, I need to abort the processing of the request around the Init event, because all I want is for the control to load the data from the ASPX file, not go through a whole request that isn't valid (because the only reason the request is being made is because MS decided I had to make one). Attempt #1 is to call Response.End...but this does nothing (still no idea why). No problem, I think to myself, I'll throw an exception and catch it on the outside...nope. You can't throw an exception inside the ProcessRequest method, without terminating your request with an exception page (regardless of whether you are catching it on the outside of the call).
So...we'll see how this turns out. Maybe the end near...
4:57:58 PM
|
|