I am currently working on .Net Core 2.2 Exception Handling scenarios.
I am using the UseDeveloperExceptionPage()
to display the exception information in Development mode.
But when I am doing an Ajax to one of the controller methods.And if the application throws a run-time exception. I am not being displayed with Exception Details by UseDeveloperExceptionPage
instead I am getting the response
in Ajax error
function(). Could anyone explain why the Developer Exception page is not being displayed.
And please explain me how to display the Developer Exception page with Exception details during ajax calls.
Below is the Configure()
in my Startup.cs
file
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseStatusCodePagesWithReExecute("/Error/{0}");
}
else
{
app.UseExceptionHandler("/Exception");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
//app.UseAuthentication();
app.UseSession();
//app.UseCookiePolicy();
app.UseMiddleware<AuthenticationMiddleware>();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Account}/{action=Index}/{id?}");
});
}
Javascript
$.ajax({
url: "@Url.Action("Index", "Home")",
dataType: "json",
type: "POST",
Async: true,
success: function(result) {
// Do Something
},
error: function() {
alert("Oops!! Error Occured");
}
});
HomeController
public IActionResult Index()
{
throw new Exception();
return View(viewTransactionsBatchSearch);
}
You can get the exception information like this:
$.ajax({
url: "@Url.Action("Index", "Home")",
dataType: "json",
type: "POST",
Async: true,
success: function (result) {
// Do Something
},
error: function (result) {
//do something with the responseText which contains the Body of the response
alert(result.responseText);
}
});
The controller does NOT know if you called it using AJAX or not... It just looks at the Http Request. You can inspect the Http Response by adding a parameter to your error function.
For example you can inspect the responseText property to get the exception information.
You could for example set the contents to a div or other display element.
Also because your AJAX request's dataType is set to "json" the reponseText will contain only the exception information plus the headers.
If you set the dataType to "html" you will get the FULL Developer Exception Page's html which you can then do something with.
Here are a couple of examples on what to do with the responseText.
Example 1 when dataType: "html":
$.ajax({
url: "@Url.Action("Index", "Home")",
dataType: "html",
type: "POST",
Async: true,
success: function (result) {
// Do Something
},
error: function (result) {
document.open();
document.write(result.responseText);//this will replace the current page with the developer exception page
document.close();
}
});
Example 2 when dataType: "json": This one is different because YOU requested JSON so the server does NOT return a full HTML Document.
This one does not look "pretty" but it has all the exception information.
$.ajax({
url: "@Url.Action("Index", "Home")",
dataType: "json",
type: "POST",
Async: true,
success: function (result) {
// Do Something
},
error: function (result) {
document.body.innerHTML = result.responseText;
}
});
dear friend, I agree with you )
Thank you very much. Is it possible to know the type of environment that the javascript is running on. Like we have
if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }
in startup.cs@Vijay You are welcome. Please mark as answer if this resolves your issue.