user2687506 user2687506 - 5 months ago 18
Ajax Question

How to return information when returning a 500 Response

I want to return a 500 Exception with information from my backend to my frontend, but everytime I choose to do a

Response.StatusCode = 500
the code seems to return the 500 Standard Return page. Is there anyway to return my own data after I choose to tell the system it was a 500-error?

[HttpPost]
public async Task<ActionResult> AccountInformation(OrganizationAccountInformationVM vm)
{
var response = await _adminFacade.UpdateOrganizationAsync(organization);

if (response.HasErrors)
{
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return Json(new {Code = "500", Header = "Error", Message = "Wrong"});
}
Response.StatusCode = (int)HttpStatusCode.OK;
return Json(new {Code = "400", Header = "Success", Message= "UpdatedSuccessful"});
}


This code will return "good" data if it has no errors, and "bad" data (a whole html-page) if the resposne HasErrors. Do you know what I'm doing wrong?

Answer

You can specify to not replace error responses using the web.config file.

<system.webServer>
   <httpErrors existingResponse="PassThrough" /> 
</system.webServer>

Take a look at system webserver httperrors