Click here to Skip to main content
15,884,099 members
Articles / Web Development / XHTML
Article

Checking Request is Synchronous or Asynchronous in ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.25/5 (3 votes)
10 Aug 2008CPOL3 min read 27.7K   11   2
How to check Request is of type synchronous or asynchronous in AJAX enabled ASP.Net application?

Introduction

NEED:

We developed web application using Asp.Net Framework 2.0, AJAX and VS 2005. Later after launch of VS 2008 and .Net Framework 3.5 we ported application to Asp.Net Framework 3.5. As needed we also upgraded AJAX toolkit which is compliant to Asp.Net 3.5. And this causes the problem as follows:

In our application we are handling error as follows:

  1. If exception occurs in normal (synchronous) request then we redirect user to generic Error Page. This code is written in Global.asax page in Application_Error event.
  2. Code
  3. If exception occurs in asynchronous request then we let AJAX Framework handle that error in its "ScriptManager1_AsyncPostBackError" event.

In case Asp.Net 2.0 and AJAX, when any exception thrown while execution of asynchronous request, it is catched in "ScriptManager1_AsyncPostBackError" event, then AJAX framework handles that exception and shows java script alert box to user. No event (Application_Error) from Global.asax file gets fired.

But, in Asp.Net 3.5 AJAX they changed exception handling mechanism and now both events "ScriptManager1_AsyncPostBackError" and Application_Error from Global.asax gets fired.

Issue: Now, if exception occurs in case of asynchronous request, application tries to redirect user to generic error page instead of showing JavaScript alert and ends up in another exception as Redirect is not allowed in asynchronous request. Ha.... exception within Application_Error method ;).

Probable Solution

In Application_Error method from Global.asax, check request is of which type. If it is of asynchronous then don't redirect to generic Error Page else redirect to generic Error Page. (Simple one right!!)

But, How to check Request is of type synchronous or asynchronous in Global.asax?

  • There is no related property in HttpRequest class.
  • There is one property called IsInAsyncPostBack in ScriptManager class but we can not browse it as it has attribute

image

  • Also, you can not access ScriptManager in Global.asax :).

SOLUTION

If you check code for setting ScriptManager's IsInAsyncPostBack (which we can not browse) property using reflector, it looks like

image

It is calling IsAsyncPostBackRequest method from PageRequestManager class (which is internal sealed class). So, we can not inherit or extend it. Then what should we do?

ADD EXTENSION METHOD : This is the solution.

So, what we did we added one extension method in HttpRequest class which does the job. Some details and example of extension method you can find here.

Some thumb rules for adding extension method are:

  • It should be static method within static class.
  • The this keyword should be in front of parameter.

Here is method implementation which looks like:

image

Now, this method is part of HttpRequest class, so you can use it like any other methods of HttpRequest class.

Using the code

Here, is code snippet which uses this extension method (this is from Global.asax):

image

So, now we can check if request is not of type asynchronous then only redirect to generic Error Page. PROBLEM SOLVED !!

Using this way now you can check whether request is synchronous or asynchronous anywhere in application.

Enjoy Programming!!!

Points of Interest

We quite often come across situation where we need to check request is synchronous or aynchronous. Strange: ASP.Net does not provide this in Framework.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAlternative with less code Pin
h23atsi28-Oct-12 9:32
h23atsi28-Oct-12 9:32 
GeneralWorks ok, here's the cut & past version. Pin
Matthew Mckenna9-Jun-09 17:43
Matthew Mckenna9-Jun-09 17:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.