Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6636161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:09:03+00:00 2026-05-25T23:09:03+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:09:04+00:00Added an answer on May 25, 2026 at 11:09 pm

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want upload file without reload page. My server-side is ASP.NET MVC. Ext.Ajax.request( {
Does anybody know how I use asp.net mvc routing to force any non ajax
I'm very new to the whole Ajax/Asp.Net stuff so... I know that there are
The Asp.Net's ajax implementation using UpdatePanel is really slow and experienced people know that
Is it possible to know that a HTTP request is from Ajax?If yes, how?
I want to know Id update panel that initial a request in JavaScript .I
I have an application in Asp.net MVC where at some point I would like
I am calling PageMethods for updating Gridview content in asp.net application [personal web app].
I know it's not possible to send an ajax request or use GWT's RequestBuidler
My javascript send Ajax request which will invoke a controller function, then the controller

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.