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.

No comments:

Post a Comment

Comments are very welcome but are moderated to prevent spam.

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