本文介绍了在MVC 4 HttpContext.Current.Request.IsAjaxRequest()错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用

  HttpContext.Current.Request.IsAjaxRequest()

状态检查中的Application_Error方法的Global.asax一个Ajax请求,但我得到下面的错误:

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.

这篇关于在MVC 4 HttpContext.Current.Request.IsAjaxRequest()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 12:45