22 Dec 2009

HttpHandler EnableEventValidation Error

I wrote a HttpHandler to do some crazy thing or other. Put it in web.config's <httpHandlers> like this:


<add path="*/Pages/Review/*/*.aspx" verb="*" type="Freda.Classes.v3ReviewScreenHandler" />


Had to handle it in IIS7 too (or integrated pipeline mode on my workstation), so had to stick it in the <system.webServer>/<handlers> section like this:


<add name="v3ReviewScreenHandler1" path="*/Pages/Review/*/*.aspx" verb="*" type="Freda.Classes.v3ReviewScreenHandler" resourceType="Unspecified" preCondition="integratedMode" />


And to make both play nicely together, under <system.webServer> I had to also add:


<validation validateIntegratedModeConfiguration="false" />


The handler in this case is basically just a subclass of System.Web.UI.Page that dynamically loads a UserControl based on the requested URL path, or otherwise processes an ASPX page, which I did like so:


public override void ProcessRequest(System.Web.HttpContext context)
{
string candidateControlFilePath = context.Request.Path.Replace(".aspx", ".ascx");

if (System.IO.File.Exists(context.Request.MapPath(candidateControlFilePath)))
{
// Load Screen Control if available (the groovy new way)
base.ProcessRequest(context);
}
else
{
// Revert to aspx mode (the non-bulk-printable way)
Page page = (Page)System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(context.Request.Path, typeof(System.Web.UI.Page));
page.AppRelativeVirtualPath = context.Request.AppRelativeCurrentExecutionFilePath;
page.ProcessRequest(context);
}
}


And the "Load User Control" bit was like so:


protected override void OnInit(EventArgs e)
{
base.OnInit(e);

// Load ReviewScreen control
string candidateControlFilePath = Request.Path.Replace(".aspx", ".ascx");
v3ReviewScreen uctl = (v3ReviewScreen)LoadControl(candidateControlFilePath);
this.Master.MainContentPlaceHolder.Controls.Add(uctl);
uctl.Setup();
}


And to make the Handler play nice with Sessions it was declared like so:


public class ReviewScreenHandler : System.Web.UI.Page, System.Web.SessionState.IRequiresSessionState


And all was dandy! Except for some funky GridView Row-Rendering stuff that was working in normal ASPX pages but not in my HttpHandler-based pages, producing this error:


RegisterForEventValidation can only be called during Render();


Which was weird, because the code to supress this sort of thing was already in web.config:


<pages enableEventValidation="false" validateRequest="false">


And I tried setting EnableEventValidation programatically in the HttpHandler's OnInit method, but got:


The 'EnableEventValidation' property can only be set in the page directive or in the configuration section.


And in the end the only solution was:


protected void Page_PreInit(object sender, EventArgs e)
{
Page.EnableEventValidation = false;
}


Which, called by magic and with no override or event handler to obviously hang off, is stupid. But works.

Mac OSX Bootcamp XP funtime

I got a Mac. I always said I wouldn't. And now I have. A little Mac Mini 2.53Ghz Intel Core 2 Duo 4Gb. Just for research, you understand ;) And cos I fell in love with my iPhone 3GS. Damn it.

What I found out so far:

- Snow Leopard is teh sexeh.
- Objective-C sucks monkey balls.

Anyway, I wanted to set up a little XP installation so I could see how well Windows runs on a Mac, and to do stuff that the mac just won't do, like Visual Studio 2008 (soooo much better than XCode).

I thought I would be smart and use the OSX Disk Utility to partition the drive 3 ways so I could have a main mac partition, a FAT32 partition to share my music on, and a NTFS partition for XP. But all my manual blunderings and attempts with Bootcamp just shagged it up, and ended up with "Disk error" on boot-up. Thank goodness for holding down the Option key on reboot to choose the boot drive. I had to use Disk Utility to create one big contiguous Mac partition again.

In essence then, to get XP running happily:

1. In OSX run Boot Camp Assistant and create a Windows Partition (I chose 32GB)
2. Put the XP disk in the drive when it tells you and start the Windows installation process.
3. When the XP installer asks what partition to put it on, choose your new Bootcamp partition and SELECT NTFS FORMAT. *Not* Quick Format, Fat32 or "Preserve Existing". It must do it the long winded way.

Anyways, it works. I was very impressed by the XP drivers Apple included on the main OSX disc. XP runs fabbo on the Mac Mini, and it picked up my Logitech wireless keyboard/mouse combo without a hitch, even though the box for them says OSX only.

Oh, almost forgot. After installing the Apple XP drivers I could read files off the main OSX drive from XP with no problems (Read Only). I thought that wasn't possible! Well chuffed.

Toodles.
If I helped you out today, you can buy me a beer below. Cheers!