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" });
}
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With