Custom Error Page not showing - asp.net

For some reason, when I get an ASP.NET runtime error, it's not loading my custom error page
<customErrors mode="On" defaultRedirect="app_offline.htm" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="app_offline.htm"/>
<error statusCode="500" redirect="app_offline.htm"/>
</customErrors>
That's in my web.config.
I'm still getting this though and it's not loading my error .htm page:
Server Error in '/' Application.
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration>

I'm pretty sure that app_offline.htm is a reserved filename in ASP.NET that will always be served if present on the server.
Try renaming it to error.htm and updating your <customErrors /> block to match.

I suspect what's happening is that ASP.NET can't find your custom error page. The path to your error page file needs to be relative or absolute. So either:
<customErrors mode="On" defaultRedirect="~/app_offline.htm" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="~/app_offline.htm"/>
<error statusCode="500" redirect="~/app_offline.htm"/>
</customErrors>
Or:
<customErrors mode="On" defaultRedirect="http://mysite.com/app_offline.htm" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="http://mysite.com/app_offline.htm"/>
<error statusCode="500" redirect="http://mysite.com/app_offline.htm"/>
</customErrors>
Should solve your problem.

Do you get details of the actual error if you set mode="Off"? If you still get the same default error page, you might find the solution in this post:
CustomErrors mode="Off"

Problem was my site was running under .NET 3.5 when it's a 4.0 app

I also had the same issue. Main problem is related to visibility of your error pages to various users. It is controlled by the tag
Mode
as shown below
I think you have used RemoteOnly . In order to show the custom error page while running in your local machine we have to make the mode value as On. For me it is worked afterwords.
Also we can give error pages for various types of error codes. For example in below figure you can see - I have given redirecting page as CustomErrorAspxPage.aspx for file not found exception type for the status code 404.
At last in order to test this I created exceptions 1)for default exception 2)for 404 file not exists exception as shown in below figure.

Related

asp.net customErrors does not react

I need help with settings. It's never use provided page for error 404.
This is my customErrors web.config block:
<customErrors mode="On" defaultRedirect="/systemerror.aspx">
<error statusCode="404" redirect="/404.aspx" />
</customErrors>
I have these page created and they works well if I call them by url, but when I write in url non-existent page like:http://www.mysite.com/asdfasdasd I am getting:
Server Error
404 - File or directory not found.
The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.
It's not use my custom error page 404.aspx, how can I fix this ?
If you are using IIS 7, try:
Response.TrySkipIisCustomErrors = true;
Refer:
HttpResponse.TrySkipIisCustomErrors Property :Gets or sets a value that specifies whether IIS 7.0 custom errors are disabled.
You are missing ~
<customErrors mode="On" defaultRedirect="~/systemerror.aspx">
<error statusCode="404" redirect="~/404.aspx" />
</customErrors>
This should work. Let me know.

.NET Compilation Error, show user friendly screen

I've configured the web.config to show user friendly 404 and 500 errors using the following declaration:
<customErrors mode="Off" redirectMode="ResponseRewrite">
<error statusCode="500" redirect="error.aspx"/>
<error statusCode="404" redirect="404.aspx"/>
</customErrors>
However occasionally when we're FTP'ing classes up to the server (in the App_Code folder) we may cause a compilation error that doesn't get handled gracefully and the user sees the yellow screen of death. Is there a way to hide this and show a standard holding page instead? Perhaps some adaptation of the web.config declaration above?
I know ideally we should be compiling first and publishing but we are doing a lot of dynamic changes at the moment, that make this difficult.
You can modify web.config in following way to show user-friendly messages on any error:
<customErrors mode="on" defaultRedirect="otherError.aspx" redirectMode="ResponseRewrite">
<error statusCode="500" redirect="error.aspx"/>
<error statusCode="404" redirect="404.aspx"/>
</customErrors>
Edit:
If you don't want to see compilation errors while updating your website, you can take it offline for the time of the update.
To do that, put a file named app_offline.htm in the root of your website.
The content of that file should state, that website is under maintenance/update. You can adapt content that it will be in the same style as your website.
When you are done with the updates, simply rename that file and website will work as supposed to.

ASP.NET / Web.config: customErrors redirect only on a 404

I have this scenario:
A user comes to my site and follows a link, which doesn't exists anymore, he should be redirected to a custom errorpage. (that works)
If a user does something, that throws an error, he should see the Stacktrace and the real Errorpage.
This is my current Web.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<customErrors>
<error statusCode="404" redirect="/errors/404.htm" />
</customErrors>
<compilation debug="true" strict="false" explicit="true" />
</system.web>
</configuration>
with this configuration, a 404 will be redirected to the right site, but a HTTP 500 will be shown as following:
Server Error in '/' Application
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a tag within a "web [.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "Off".
[...]
But in this case I want to show the stacktrace.
How can I do this?
Note: We're on Linux with a Mono <- FastCGI -> Lighttpd construction.
In the following web.config entries, a not found (404) condition will send a user to PageNotFound.aspx
Use mode="Off" and everyone (local and remote users) will see error details.
<customErrors mode="Off">
<error statusCode="404" redirect="~/errorPages/PageNotFound.aspx" />
</customErrors>
Use mode="RemoteOnly" and local users will see detailed error pages with a stack trace and compilation details. Remote users with be presented with the GeneralError.aspx page
<customErrors mode="RemoteOnly" defaultRedirect="~/errorPages/GeneralError.aspx">
<error statusCode="404" redirect="~/errorPages/PageNotFound.aspx" />
</customErrors>
Ray Van Halens Answer is correct, but this was not the actual problem.
The reason for not showing stacktrace is a bug in mono itself. There is no other way then write an own error page where the stacktrace is dispayed.

ASP.net Server Error in '/' Application

I've had users consistently getting this error on our homepage:
Server Error in '/' Application.
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration>
I can't reproduce this error at all live or on my dev server. However, in my web.config file I beleive I do have the error code setup correctly:
<customErrors mode="On" defaultRedirect="~/error.aspx" redirectMode="ResponseRewrite">
<error statusCode="500" redirect="~/error.aspx" />
</customErrors>
All other errors on the site seem to direct to error.aspx just fine, does anyone have any idea why this error would be occurring and how I can catch it?
You should look in your Application Log in the Event Viewer to see what the actual error message is. Once you find the error message - you can determine why it is not redirecting.
Another option (albeit dangerous for public sites) is to change the error handler to display the actual error message with stack trace so you can troubleshoot the problem (customErrors mode='Off'). The users could then deliver the actual error to you instead of the generic error page above.
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>

Why setting customErrors in web.config doesn't work at this case?

In my ASP.NET 3.5 Website which is published in shared hosting provider , I've configured my web.config file like this :
<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="AccessDenied.htm"/>
<error statusCode="404" redirect="FileNotFound.htm"/>
</customErrors>
If the user request pages that doesn't exist ( like "www.example.com/NotExistPage.aspx" ) , user will be redirected to FileNotFound.htm page as we expect .
But if the user request some address like : "www.example.com/NotExistDirectory" without .aspx extension , the user will encounter IIS 7.5 Error page :
HTTP Error 404.0 - Not Found The
resource you are looking for has been
removed, had its name changed, or is
temporarily unavailable.
Detialed error information :
Module IIS Web Core
Notification MapRequestHandler
Handler StaticFile
Error Code 0x80070002
Requested URL http://www.example.com:80/NotExistDirectory
Physical Path D:\Websites\example\example.com\wwwroot\NotExistDirectory
Logon Method Anonymous
Logon User Anonymous
This is a yellow page which is not user friendly and we didn't expect .
I'm wondering setting customeError in webconfig doesn't support this type of address or not ? How can i prevent users seeing this yellow page .
Edit :
Thanks to David's answer , But I found the actual reason and correct solution. Please see my answer.
#Mostafa: I faced the exact same problem. I found out it can be solved by adding the following to the web.config file:
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" subStatusCode="-1" prefixLanguageFilePath="" path="/MyErrorPage.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
Thanks to #David solution, But the reason and complete solution are as below:
By setting customErrors mode to "On" it's only working when we get an exception in ASP.NET application, While when we trying to reach nonExistingdirectory Or notExsitingStaticResouce, IIS renders a 404 error and it does not reach to asp.net runtime and served by IIS directly.
So we need to add configuration for IIS as below in Web.config:
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="~/404.html" responseMode="File" />
</httpErrors>
<system.webServer>
It's important to set responseMode to "File", Otherwise status code automatically would change from 404 to 200. So from the client's perspective, they don't get the actual 404 status code.
This is because the ASP.Net module is configured to handle certain file extensions. IIS determines that .aspx must be handled by the ASP.Net module and then the customerrors section in the web.config ( and indeed web.config itself) kicks in.
Since you have requested a page not even configured for ASP.Net, IIS handles it on its own without passing the request on.
For any other files other than .aspx, you can configure this in IIS:
http://www.xefteri.com/articles/show.cfm?id=11
First, directory url must posess trailing slash, otherwise its just an extensionless file.
www.mysite.com/NotExistDirectory/
Second, ASP.net IIS module is handler for ASP MIME types only, so directory is leftover for web server.
Third, customerror is part of system.web is part of ASP.net configuration
and httperror is part of system.webserver is part of IIS configuration.
Assuming http module defaults in IIS configuration httperror will work with custom error for non-existing directory.

Resources