3

Ive added custom error pages to my project, and referenced them in the web.config like so:

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Errors/500.aspx">
  <error statusCode="404" redirect="~/Errors/404.aspx" />
  <error statusCode="500" redirect="~/Errors/500.aspx" />
</customErrors>

When I browse to a page that does not exist I see the 404 page rendered as expected.

However, I now have a bug in the following POST controller method:

CheckRenewalEligibility(string uprn);

When I click the button which invokes this, nothing happens. When I open dev tools in chrome I can see:

enter image description here

And then when I click on the red highlighted CheckRenewalEligibility to see the Preview, it displays my custom error page.

My question is, why is this not being rendered in the front end when a 500 error is clearly occurring, why is it just doing nothing.

note I have an aspx and html version of both custom error pages sat in the Errors path.

JsonStatham
  • 9,770
  • 27
  • 100
  • 181
  • 1
    Are you doing the POSTing via AJAX? – trailmax Sep 24 '15 at 09:48
  • CheckRenewalEligibility will return a partial view with the required viewmodel populated. – JsonStatham Sep 24 '15 at 09:55
  • In that case reply to your request is the `500.aspx` page - but the page never displayed to the user because the page is not reloaded. – trailmax Sep 24 '15 at 09:56
  • Is there a way of handling this scenario whilst being consistent to the non AJAX posts in terms of custom errors? – JsonStatham Sep 24 '15 at 10:05
  • In your AJAX requests you can check if reply was not success (4xx or 5xx HTTP status in reply) and show a message to a user or redirect them to a 500.aspx page. – trailmax Sep 24 '15 at 10:07
  • 1
    @trailmax, thanks for you help, could you please roll your comments into an Answer and I will mark accordingly. – JsonStatham Sep 24 '15 at 10:37

1 Answers1

1

The issue you are having is because you do your request via AJAX. In these cases in server side errors, error pages are rendered, but never displayed to a user - that is the nature of AJAX.

Since you have not indicated the way you do your AJAX execution, I'll presume you are using popular jQuery. For ajax requests via jQuery have a look on this answer - explains how to capture server-side errors.

Community
  • 1
  • 1
trailmax
  • 34,305
  • 22
  • 140
  • 234