[转]ASP.NET AJAX and Sys.Webforms.PageRequestManagerServerErrorException
Posted on 2011-08-11 00:01 Leo.W 阅读(308) 评论(0) 编辑 收藏 举报ASP.NET AJAX and Sys.Webforms.PageRequestManagerServerErrorException
by Ted JardineUsing ASP.NET AJAX extensively in my latest project I've been sporadically running into the Sys.WebForms.PageRequestManagerParserErrorException. It got to the point that I was contemplating ripping out ASP.NET AJAX completely until this known issue had been ironed out. The various causes for this error are mentioned many different places, but for some samples, go here, here, and here.
Quoting from Eilon Lipton's blog posting, this particular exception is very common and can be caused by any one of the following:
- Calls to Response.Write():
By calling Response.Write() directly you are bypassing the normal rendering mechanism of ASP.NET controls. The bits you write are going straight out to the client without further processing (well, mostly...). This means that UpdatePanel can't encode the data in its special format. - Response filters:
Similar to Response.Write(), response filters can change the rendering in such a way that the UpdatePanel won't know. - HttpModules:
Again, the same deal as Response.Write() and response filters. - Server trace is enabled:
If I were going to implement trace again, I'd do it differently. Trace is effectively written out using Response.Write(), and as such messes up the special format that we use for UpdatePanel. - Calls to Server.Transfer():
Unfortunately, there's no way to detect that Server.Transfer() was called. This means that UpdatePanel can't do anything intelligent when someone calls Server.Transfer(). The response sent back to the client is the HTML markup from the page to which you transferred. Since its HTML and not the special format, it can't be parsed, and you get the error.
The problem was I wasn't doing any of the above (who uses Response.Write in an ASP.NET application these days?) and I was still sporadically encountering the error - a show stopping error I might add. An error that is popped up in a javascript warning box completely undecipherable to the end user leaving an empty/useless/castrated UpdatePanel in its wake. This of course leaves the end user feeling likewise empty/useless/castrated (to say nothing of the developer).
This post here indicates that there is a problem with the RoleMangerModule or any time you set a cookie to the response in an AJAX callback, which can only be solved by doing one of the following:
- Disable caching of cookies in the RoleManager. (yuck)
- Handle the Application's Error event to clear the error from the Context selectively (eek).
- Disable EventValidation in the web form.
<%@ Page Language="C#" EnableEventValidation="false" %>
(gulp)
None of the above are entirely reasonable solutions (especially the last two), and the worst part was that my test page was just a simple contact page that did not change/set roles or cookies, or response.write, or set anything in the session, and wasn't receiving any Unicode character input, or even requiring a user to be logged in, or writing anything to the trace, or anything beyond the basics. And still it blew up. But only occasionally.
In order to faithfully reproduce the error, I finally determined that it must have something to do with sessions as it would only occur if the app pool had recycled and all browser windows had been closed. So, based on one of the comments in one of the above posts, even though I'm not touching session on one of the problem pages, I tried a hack in one of the problem page's Page_Load:
Session["FixAJAXSysBug"] = true;
And lo and behold, we're good to go! So even though I am not using Session on the problem page it must be attempting to set the initial session cookie using the Update Panel callback. So the solution is to make sure the initial session is set before any Update Panel callback takes place. How this got through into production is beyond me.
So if you're sporadically encountering the Sys.Webforms.PageRequestManagerServerErrorException, it could be for any of the above reasons or the fact that your dog/cat/stuffed teddy bear is sitting too close to your monitor. But give the last one a try in every page utilizing AJAX if you're using sessions in your application.
UPDATE: If the problem pages aren't even using session, just turn session off for the page:
<%@ Page EnableSessionState="false" ... %>
Or better yet, set it in your base class to always be off, and turn it on for the pages where you need it on.