我在用
HttpContext.Current.Request.IsAjaxRequest()
在Application_Error方法中检查global.asax中的ajax请求的条件,但是我收到以下错误:
‘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
以下是代码:
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" }); } }
解决方法
猜猜它有用……发布作为答案.
尝试
new HttpRequestWrapper(System.Web.HttpContext.Current.Request).IsAjaxRequest()
IsAjaxRequest()接受一个与HttpRequest不同的HttpRequestBase(并且不相关,所以它有点令人困惑).我认为包装器将解决您的问题.