【发布时间】:2013-01-15 18:17:38
【问题描述】:
我正在使用
HttpContext.Current.Request.IsAjaxRequest()
在 Application_Error 方法中检查 global.asax 中的 ajax 请求的条件,但我收到以下错误:
'System.Web.HttpRequest' 不包含对 'IsAjaxRequest' 和最好的扩展方法重载 'System.Web.Mvc.AjaxRequestExtensions.IsAjaxRequest(System.Web.HttpRequestBase)' 有一些无效的参数
下面是代码:
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" });
}
}
【问题讨论】:
-
修改问题添加代码。
-
Try
new HttpRequestWrapper(System.Web.HttpContext.Current.Request).IsAjaxRequest()IsAjaxRequest()采用与 HttpRequest 不同的 HttpRequestBase(并且不相关,因此有点混乱)。我认为包装器会解决你的问题。
标签: asp.net-mvc-4