Response.Redirect issue with Asp.net async

I’m new to asp.net 4.5 async and am running into the following with calling response.redirect within an async method. The issue is that the response just “hangs” Has anyone else experienced similar issues with attempting an redirect with async? This code will work in a brand new project, but, does not work with a new page in our existing code. I made sure to gut out everything I could out of our web.config and removed our master page. Hitting a brick wall…any ideas? Thanks!

    protected void Page_Load(object sender, EventArgs e)
    {
        RegisterAsyncTask(new PageAsyncTask(PageLoadAsync));
    }

    private async Task PageLoadAsync()
    {
        var data = await GetData();

        if (data == HttpStatusCode.OK)
            Response.Redirect("http://www.google.com");
    }

    private async Task<HttpStatusCode> GetData()
    {
        using (var client = new HttpClient())
        {
            var response = await client.GetAsync("https://www.google.com");
            return response.StatusCode;
        }
    }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This code will work in a brand new project, but, does not work with a new page in our existing code.

I assume your existing site has already been upgraded to .NET 4.5.

The first thing to check is that httpRuntime.targetFramework is set to 4.5. This is not set by default when you upgrade.

Edit from comments:

Another thing to check (just in case) is that Page.Async is set to true.

In this case, the solution was to call Response.Redirect("http://www.google.com", false), which explicitly passes false for the endResponse parameter. The default value of true is only for backwards-compatibility reasons as described here.

Method 2

The hack I used is:

  1. I used a static dictionary as var d= new Dictionary<string, bool>(); in the class where my API calling method is written.
  2. I put the code line client.timeout = new System.TimeSpan(0,0,60); for API sending the request.
  3. When API is timed out, it throws the TaskTimeoutException, in the TaskTimeoutExceptioncatch block write code as d.Add("timeout", true);
  4. Now, I created the custom action filter and applied the following code:
    public class MyCustomActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if(MyApiClass.d.ContainsKey("timeout") && d["timeout"])
            {
                throw new Exception();
            }
        }
    }
  5. I applied the [MyCustomActionFilter ] on the action.
  6. When action is executed and enter the custom filter it throws Exception by checking the dictionary entry.
  7. If timeout would have occurred then dictionary entry will be true, so, on the basis of that, we check the entry and throws the exception. Now, we have Application_Error() in Global.asax.cs that catches the exception.
  8. In the Application_Error() we have written the code for redirect to the required page.

NOTE: In step 4 you can create your custom exception to provide more precise detail for logging.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x