The subject is selfexplanatory. I've developer and production environments. Developer env. is my localhost machine. I've action methods in contolers that sets response status code to 500 when something wents wrong (error occured, or logical inconsistance) and returns Json-answer. My common method looks like that:
[HttpPost]
public ActionResult DoSomething(int id)
{
try
{
// some useful code
}
catch(Exception ex)
{
Response.StatusCode = 500;
Json(new { message = "error" }, JsonBehaviour.AllowGet)
}
}
On the client side in production env. when such an error occured ajax.response looks like an HTML-code, instead of expected JSON.
Consider this:
<div class="content-container">
<fieldset>
<h2>500 - Internal server error.</h2>
<h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
</fieldset>
</div>
Filter context is not an option. I think it is some sort of IIS or web.config issue.
SOLUTION:
We decided to add TrySkipIisCustomErrors
in BeginRequest in Global.asax and it is solved problems in each method in our application.
I guess that IIS is serving some friendly error page. You could try skipping this page by setting the TrySkipIisCustomErrors
property on the response:
catch(Exception ex)
{
Response.StatusCode = 500;
Response.TrySkipIisCustomErrors = true;
return Json(new { message = "error" }, JsonBehaviour.AllowGet)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With