This will keep the original URL but still display a custom page. Set the redirectMode attribute to ResponseRewrite. Use the redirectMode attribute if you want to preserve the original URL without ?aspxerrorpath query string.īy default, it has redirectMode set to ResponseRedirect, and that's why the URL gets changed when an exception occurred. You can preserve the original URL using the redirectMode attribute. The above settings will display the following result when navigating to a non-existent page /home/test.Īs you have seen, URLs include a query string ?aspxerrorpath query string when a custom error page displays. Of course, you have to create all the above action methods and their views. Otherwise, it will redirect to the default Error.cshtml page, not the configured one. You have to remove HttpErrorHandler filter if you use the defaultRedirect attribute. This will redirect to this page on any error code, not just 500. You can specify the name of a webform, an HTML, or an action method. Use the defaultRedirect to specify where an http request should be redirect by default if an error occurs. This setting will only show a custom page for 500 level errors, but not for other error codes. You will see it has responded with the status code 500. The HandleErrorAttribute filter set this default error view in ASP.NET MVC.Ĭheck the status code of the response in a developer console by pressing F12 (in Chrome) and refresh the page again. Well! It has rendered Error.cshtml view from the Shared folder. Surprised! How it shows the above page, and from where? Ideally, we should use RemoteOnly.Īfter enabling the customErrors mode to On, an ASP.NET MVC application will show the default custom error page, as shown below. We will use Mode=On to test it on the localhost. RemoteOnly: Specifies that custom errors are shown only to remote clients, and ASP.NET errors are shown to the localhost. Off: Specifies that custom errors are disabled. If no defaultRedirect is specified, users see a generic error page e.g. On: Specifies that custom errors are enabled. The first step is to enable customErrors using its mode attribute that can have one of the following three values: We can render or redirect to a custom page using section of web.config file. Now, navigate to /home/contact in the browser, and you will see the following yellow page (also known as the Yellow Screen of Death) that shows exception details such as exception type, line number and file name where the exception occurred, and stack trace.Īll the unhandled exceptions should be handled and display a meaningful message to the users instead of error information.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |