简体   繁体   English

如何在Application_Error()中知道asp.net中的请求是ajax

[英]How to know if the request is ajax in asp.net in Application_Error()

How to know if the request is ajax in asp.net in Application_Error() 如何在Application_Error()中知道asp.net中的请求是ajax

I want to handle app error in Application_Error().If the request is ajax and some exception is thrown,then write the error in log file and return a json data that contains error tips for client . 我想在Application_Error()中处理应用程序错误。如果请求是ajax并且抛出了一些异常,则在日志文件中写入错误并返回包含客户端错误提示的json数据。 Else if the request is synchronism and some exception is thrown ,write the error in log file and then redirect to a error page. 否则,如果请求是同步并且抛出了一些异常,请在日志文件中写入错误,然后重定向到错误页面。

but now i cant judge which kind the request is . 但现在我无法判断请求是哪种。 I want to get "X-Requested-With" from header ,unfortunately keys of headers don't contain "X-Requested-With" key ,why? 我想从标题中获取“X-Requested-With”,遗憾的是标题的键不包含“X-Requested-With”键,为什么?

Testing for the request header should work. 测试请求标头应该有效。 For example: 例如:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult AjaxTest()
    {
        throw new Exception();
    }
}

and in Application_Error : 并在Application_Error

protected void Application_Error()
{
    bool isAjaxCall = string.Equals("XMLHttpRequest", Context.Request.Headers["x-requested-with"], StringComparison.OrdinalIgnoreCase);
    Context.ClearError();
    if (isAjaxCall)
    {
        Context.Response.ContentType = "application/json";
        Context.Response.StatusCode = 200;
        Context.Response.Write(
            new JavaScriptSerializer().Serialize(
                new { error = "some nasty error occured" }
            )
        );
    }

}

and then send some Ajax request: 然后发送一些Ajax请求:

<script type="text/javascript">
    $.get('@Url.Action("AjaxTest", "Home")', function (result) {
        if (result.error) {
            alert(result.error);
        }
    });
</script>

您还可以在包含方法IsAjaxRequest的HttpRequestWrapper中包装Context.Request(类型为HttpRequest)。

bool isAjaxCall = new HttpRequestWrapper(Context.Request).IsAjaxRequest();

You could use this. 你可以用它。

    private static bool IsAjaxRequest()
    {
        return HttpContext.Current.Request.Headers["X-Requested-With"] == "XMLHttpRequest";
    }

it is possible to add custom headers in the client side ajax call. 可以在客户端ajax调用中添加自定义标头。 Refer http://forums.asp.net/t/1229399.aspx/1 请参阅http://forums.asp.net/t/1229399.aspx/1

Try looking for this header value in the server. 尝试在服务器中查找此标头值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从ASP.NET MVC 2中的Application_Error重定向? - How to redirect from Application_Error in ASP.NET MVC 2? asp.net中的Application_Error与Context_Error - Application_Error vs Context_Error in asp.net ASP.NET MVC如何在不重写URL的情况下处理Application_Error的404错误 - ASP.NET MVC how to handle 404 error from Application_Error without rewriting the URL 即使Application_Error完成,asp.net应用程序仍保持运行 - asp.net application keeps running even after Application_Error finishes 使用路由的ASP.NET 4 Web窗体-错误时未调用Application_Error - ASP.NET 4 Web Forms Using Routing - Application_Error Not Called on Error Application_Error中的ASP.net Global.asax错误详细信息 - ASP.net Global.asax error detail in Application_Error ASP.NET MVC自定义错误处理Application_Error Global.asax? - ASP.NET MVC Custom Error Handling Application_Error Global.asax? 您是否应该在ASP.NET MVC的global.asax中使用方法Application_Error? - Should you use the method Application_Error in your global.asax in ASP.NET MVC? 在ASP.NET MVC4中,application_error哪里去了? - In ASP.NET MVC4, where has application_error gone? ASP.NET Web 服务忽略 Application_Error 和 UnhandledException 事件 - ASP.NET Web service ignoring Application_Error and UnhandledException event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM