How to have AJAX error direct to Developer Exception Page in ASP.NET Core MVC?

Lukas

Let's say I have an ajax function and an exception is thrown in the SomeAction action:

function CallAjaxFunction() {
    $.ajax({
        url: '/Home/SomeAction',
        success: function (response) {
            //do something
        },
        error: function (response) {
            //redirect to Developer Exception Page to show detailed error
        }
    });
}

I know that I can redirect to an error page with window.location = "/Home/Error";, but how would I redirect the browser to the Developer Exception Page to show the more detailed information?

I may be misunderstanding something fundamental here (which is a big reason why I'm asking this question), so I'd appreciate any clarification of concepts.

That page usually looks something like this: enter image description here

Fei Han

how would I redirect the browser to the Developer Exception Page to show the more detailed information?

From this doc about "Handle errors in ASP.NET Core", you would find:

Enable the Developer Exception Page only when the app is running in the Development environment. You don't want to share detailed exception information publicly when the app runs in production.

Normally, for better user experience, we do not display error page to client user directly.

If you really want to show browser user the Developer Exception Page while ajax request failed with some error, you can refer to the following sample.

$.ajax({
    url: '/Home/MyTestAction',
    dataType: "html",
    success: function (response) {
        //do something
    },
    error: function (response) {
        //console.log(response);
        document.write(response.responseText);
    }
});

Code of MyTestAction

public IActionResult MyTestAction()
{
    Convert.ToDateTime("hello");
    return Ok("success");
}

Test Result

If above error callback function executed, the content with detailed error message as below will be displayed in page

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to refresh data without page reloading in Asp.net core MVC using Ajax?

Global exception/error handling in asp.net mvc core and dapper

Redirect to error page from ajax call in ASP.net MVC

How to properly handle AJAX errors in ASP.NET Core MVC?

How to add page list in asp.net core MVC application?

How to change the default page in ASP.NET Core MVC?

Exception handling ASP .NET Core MVC 6

Login Page with ASP.NET Core (MVC)

HTTP ERROR : 405(This page isn't working) When I Refresh The Page(ASP.Net Core MVC)

How to pass the exception from exception handling middleware to ErrorController in ASP.NET Core MVC web application?

Custom error page for Http error 404.13 ASP.NET Core MVC

ASP.NET Core MVC generates an exception error when referencing a database connection

How set a identity scaffolding item/page how initial page in asp.net MVC core?

ASP.NET MVC Ajax Error handling

Redirect to Home/Error View Page in ASP.NET Core 2.0 MVC

Localhost not found error appears when trying to go to a different page - ASP.NET CORE MVC

How to use jQuery Ajax autocomplete in ASP.NET Core MVC with ADO.NET

How do I solve a "view not found" exception in asp.net core mvc project

How to have multiple HTML static pages on a ASP.NET MVC project using the Layout page?

How to have a shared environment variable for Angular 2 and ASP.NET Core MVC

ASP .NET MVC - return Exception message to ajax call

ASP.NET Core MVC throws an exception on select

ASP.NET Core 2 MVC Global Exception Handling not working

ASP.NET Core MVC exception when using AddSignInManager

ASP.NET Core MVC exception on add migration on 6.0

How to implement Localization ASP net CORE MVC

How to update database with ajax/javascript without refreshing page asp.net mvc

ASP.NET MVC: How do I change where Ajax.BeginForm will post to after the page loads?

how to add model to razor page with jquery ajax in asp.net mvc 5?