0

I need to have a custom error page to handle all http errors. When an http error happens, page should show the custom page, and it's content must be generated dynamically based on the http error status code. I half managed to show a custom html when a specific http error occurs. For example 404. But there's two problems. 1. I need to redirect to a .cshtml, so I can do some processing on the response and create the content accordingly to the http error status code. 2. I need a method to redirect all errors to this .cshtml file.

I've read docs and searched through similar questions in SO, followed them all and no progress.

Currently I have the following lines in web.config :

    <httpErrors errorMode="Custom" defaultResponseMode="File" defaultPath="CustomError.htm">
      <clear />
      <error path="CustomError.htm" responseMode="File" statusCode="403"/>
    </httpErrors>

This will show the content of the file CustomError.htm which is located in the same path of the file web.config. But as I read the docs I expected this config to work:

    <httpErrors errorMode="Custom" defaultResponseMode="File" defaultPath="CustomError.htm">
      <clear />
    </httpErrors>

But it does not show the contents of CustomError.htm file, instead it shows a blank page with the following sentence: You do not have permission to view this directory or page. Also even for handling a single httpError, I have trouble using a .cshtml and I had to use a html file. I actually have the following cshtml file: Views/Error/PageNotFound.cshtml It has a controller and an actionResult method called: PageNotFound Now, when I do this:

    <httpErrors errorMode="Custom" defaultResponseMode="File" defaultPath="CustomError.htm">
      <clear />
      <error path="~/Error/PageNotFound" responseMode="ExecuteURL" statusCode="404"/>
    </httpErrors>

Then, the browser throws a not found error.

When I change responseMode to Redirect, then the path gets appended to the existing url over and over and won't work. As you can see in the tags, I'm using asp.net mvc and IIS 10 on windows 10. So any help?

arm
  • 82
  • 1
  • 15

2 Answers2

1

I'm not 100% sure that i undersood you correctly but try this code out is use it for my custom erros and I'm 100% sure that it works

<customErrors mode="Off" defaultRedirect="/Error/Default">
  <error statusCode="404" redirect="/Error/E404" />
  <error statusCode="500" redirect="/Error/E500" />
  <error statusCode="403" redirect="/Error/E403" />
</customErrors>

you can ofc add more status codes as you go on

  • For more information please visit the following thread https://stackoverflow.com/questions/2480006/what-is-the-difference-between-customerrors-and-httperrors – Nasar Eddaoui Jun 02 '19 at 16:28
  • customErrors won't work in IIS 7.5+ for static pages. – arm Jun 03 '19 at 04:17
  • also does defaultRedirect work for you? For example if you had a 403 error, will the content of the /error/default show up, or youll get the default page? – arm Jun 03 '19 at 04:21
  • defaultRedirect will be showen on all errors if not listed if you wish to show a specific error page for 403 you will need to add the following code to customerrors – Nasar Eddaoui Jun 03 '19 at 05:05
0

@arm, Please go through below links

https://www.codeproject.com/Articles/850062/Exception-handling-in-ASP-NET-MVC-methods-explaine

I hope, Override “OnException” method may help you to sort out your issues.

Jinto John
  • 365
  • 4
  • 22