Click here to Skip to main content
12,457,938 members (51,471 online)
Click here to Skip to main content
Add your own
alternative version

Stats

19K views
11 bookmarked
Posted

Checking Request is Synchronous or Asynchronous in ASP.NET

, 10 Aug 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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 Wink | ;) .

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 Smile | :) .

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)

Share

About the Author

No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionAlternative with less code Pin
h23atsi28-Oct-12 9:32
memberh23atsi28-Oct-12 9:32 
GeneralWorks ok, here's the cut & past version. Pin
Matthew Mckenna9-Jun-09 17:43
memberMatthew 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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 10 Aug 2008
Article Copyright 2008 by Member 3422877
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid