Is it possible to prevent a request from going to the page?
This might be really hacky, but I'm beginning to get desperate
Is it possible to prevent a request from actually hitting the actual page...ie, can I write something in Application_BeginRequest that processes the part that I want, and then skips the rest of the lifecycle?
Oh, and is it possible to do it in such a way that the 开发者_C百科ajax update panel does throw a fit (return some default content that says, "I haven't done anything sorry")
Ok, mad question over.
Sure, just call Response.End():
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.Url.AbsolutePath.Equals("/PageYouWantToSuppress.aspx", StringComparison.OrdinalIgnoreCase))
{
// Do your stuff
Response.End();
}
}
ETA: I'm not sure about the UpdatePanel part of your question.
You can prevent portions of your code from executing during a partial-page update like this:
if (! ScriptManager.IsInAsyncPostBack) {
// Code in here won't happen during UpdatePanel actions
}
UpdatePanels execute most of the ASP.NET page lifecycle by design. If you abort that process (via Response.End()
for example), the request doesn't complete, so the client won't get the data it needs to update the page's HTML.
When you trigger a server-side method in an UpdatePanel ASP.NET does the following:
- Rebuilds the portion of the page within the panel from scratch by running through the normal ASP.NET page lifecycle (well, slightly abridged, but close).
- Sends the panel's HTML back to the client.
- Rebuilds the panel on the client using javascript DOM manipulation.
(For more see my recent answer to this question, and see UpdatePanel Control Overview or this article for details)
There are several alternatives to the UpdatePanel AJAX implementation that don't involve going through the whole page lifecycle. One of the most popular is Using jQuery to directly call ASP.NET AJAX page methods.
Using jQuery for AJAX calls gets you a little closer to the metal (so to speak) than UpdatePanels, but there's tons of good help to get you started - on StackOverflow itself and on Encosia (the source of that last link).
Consider writing an HTTPModule sitting on the front of the request processing flow to take care of that.
精彩评论