9

I am using

HttpContext.Current.Request.IsAjaxRequest() 

condition to check for an ajax request in global.asax in Application_Error method but I get the below error:

'System.Web.HttpRequest' does not contain a definition for 'IsAjaxRequest' and the best extension method overload 'System.Web.Mvc.AjaxRequestExtensions.IsAjaxRequest(System.Web.HttpRequestBase)' has some invalid arguments

Below is the code:

void Application_Error(object sender, EventArgs e)
    {

        Exception exception = Server.GetLastError().GetBaseException();
        HttpException httpException = exception as HttpException;

        string ErrorMessage = "";
        ErrorMessage = "Application Level Error";


        logger.Error(ErrorMessage, exception);

        if (System.Web.HttpContext.Current.Request.IsAjaxRequest()) //if its an ajax do not redirect
        {
            return;
        }
    else
    {
      Server.ClearError();
      this.Response.RedirectToRoute("Default", new { controller = "Home", action = "Error" });
    }
  }
2
  • modified the question to add the code. Commented Jan 31, 2013 at 15:36
  • 2
    Try new HttpRequestWrapper(System.Web.HttpContext.Current.Request).IsAjaxRequest() IsAjaxRequest() takes an HttpRequestBase which is different from an HttpRequest (and not related, so it's a bit confusing). I think the wrapper will fix your problem. Commented Jan 31, 2013 at 15:46

2 Answers 2

29

Guess it worked... Posting as the answer.

Try

new HttpRequestWrapper(System.Web.HttpContext.Current.Request).IsAjaxRequest() 

IsAjaxRequest() takes an HttpRequestBase which is different from an HttpRequest (and not related, so it's a bit confusing). I think the wrapper will fix your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that HttpRequestWrapper is in the System.Web namespace but IsAjaxRequest() is an extension method contained in the System.Web.Mvc assembly, so you will need to include both in your namespace references
1

In my case, I resorted to using a static method (I was in an IRouteConstraint Implementation)

bool isAjax = AjaxRequestExtensions.IsAjaxRequest(httpContext.Request);

For this to work, don't forget to include System.Web.Mvc if you don't have it already.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.