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

发布于 2024-12-06 08:45:21 字数 269 浏览 4 评论 0原文

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

我想在Application_Error()中处理应用程序错误。如果请求是ajax并且抛出一些异常,则将错误写入日志文件并返回json数据其中包含客户端的错误提示。 否则,如果请求是同步的并且抛出一些异常,则将错误写入日志文件,然后重定向到错误页面。

但现在我无法判断请求是哪一种。我想从标头获取“X-Requested-With”,不幸的是标头的键不包含“X-Requested-With”键,为什么?

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

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 .
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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事未完 2024-12-13 08:45:21

对请求标头的测试应该有效。例如:

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

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

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" }
            )
        );
    }

}

,然后发送一些 Ajax 请求:

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

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:

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:

<script type="text/javascript">
    $.get('@Url.Action("AjaxTest", "Home")', function (result) {
        if (result.error) {
            alert(result.error);
        }
    });
</script>
梦情居士 2024-12-13 08:45:21

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

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

You can also wrap the Context.Request (of the type HttpRequest) in a HttpRequestWrapper which contains a method IsAjaxRequest.

bool isAjaxCall = new HttpRequestWrapper(Context.Request).IsAjaxRequest();
追星践月 2024-12-13 08:45:21

可以在客户端 ajax 调用中添加自定义标头。请参阅http://forums.asp.net/t/1229399.aspx/1

尝试在服务器中查找此标头值。

it is possible to add custom headers in the client side ajax call. Refer http://forums.asp.net/t/1229399.aspx/1

Try looking for this header value in the server.

还在原地等你 2024-12-13 08:45:21

你可以用这个。

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

You could use this.

    private static bool IsAjaxRequest()
    {
        return HttpContext.Current.Request.Headers["X-Requested-With"] == "XMLHttpRequest";
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文