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.
Related
I have been trying to configure Custom Error pages in ASP.NET MVC 5 but with little complete success.
To do this, I have followed this guide: http://benfoster.io/blog/aspnet-mvc-custom-error-pages
Web.config customerrors
<customErrors mode="On" defaultRedirect="~/500.aspx" redirectMode="ResponseRewrite">
<error statusCode="401" redirect="~/401.aspx" />
<error statusCode="403" redirect="~/401.aspx" />
<error statusCode="404" redirect="~/404.aspx" />
</customErrors>
Web.config httpErrors
<httpErrors errorMode="Custom" >
<remove statusCode="401" />
<error statusCode="401" path="401.html" responseMode="File" />
<remove statusCode="403" />
<error statusCode="403" path="401.html" responseMode="File" />
<remove statusCode="404" />
<error statusCode="404" path="404.html" responseMode="File"/>
<remove statusCode="500" />
<error statusCode="500" path="500.html" responseMode="File" />
</httpErrors>
I have removed the HandleErrorAttribute from filter config.
With this setup, I get the following results:
Browse to a non-existent static resource
Example: http://my.website.com/fakeresource.htm
This results in the static 404.htm file configured in the httpErrors section.
The Url is preserved in the browser address bar
A 404 status code is correctly set.
Browse to a bad route
Example: http://my.website/realcontroller/fakeaction
This results in a HttpException being thrown when a matching route cannot be found.
The 404.aspx page is displayed.
The Url is preserved in the browser address bar.
A 404 status code is correctly set.
Browse to a valid route, but with an invalid ID for a database resource
Example: http://my.website.com/realcontroller/realaction/22
(22 is the ID of a resource which does not exist, the Action will return the HttpNotFound() method.
This results in the static 404.htm file configured in the httpErrors section.
The Url is preserved in the browser address bar
A 404 status code is correctly set.
If I throw HttpException(404, "Not Found") instead of using HttpNotFound(), I get the 404 defined in customErrors
Exceptions & Unauthorised Requests
Also all work correctly using this configuration of Web.config
However, any action which checks ModelState with ModelSate.IsValid, or adds errors using ModelState.AddModelError("InputName", "Error Message"); results in a 400 Bad request.
Here are the errors I get in IE & Chrome:
IE
Chrome
Instead, what I should be getting is the original view, with the validation messages displayed next to the inputs which failed validation.
If I remove the httpErrors section from web.config, the issue is resolved, but I no longer get custom IIS error pages.
I have also tried every option of the existingResponse property on httpErrors but different options breaks either the error pages or the ModelState validation.
Finally I tried setting Response.TrySkipIisCustomErrors = true; and this appear to fix the problem in the action I was testing.
But this means that I have to set this property on every action which uses ModelState, and that just doesn't sound right.
What am I doing wrong?
UPDATE
To test if this was project specific, I created a new project in VS2012 and configured the same error pages. Frustratingly, everything worked. The only difference I can think of now is that the project experiencing the problem originally started out using MVC 4, and was upgraded to MVC 5.
It would appear that custom errors had nothing to do with the bad request I encountered.
Instead, there was another form on the page which was incorrectly also being called when the main form was being posted.
We are migrating some Classic ASP sites from an IIS6 box to a new Server 2008 box running IIS7.
We have been through a learning process with regard to custom errors and now have these working correctly and Server.GetLastError is now working.
The sites we are migrating use a bespoke CMS that utilises a custom 404.asp error page to pull content from a database depending on the URL. This, too, works perfectly.
However, when the 2 are combined (e.g. we have a 500 error on a page that runs via the custom 404 page) we receive a completely blank page. No error, no information nothing. Just a plain white page.
Example 1: http://snavebelac.com/thisdoesnotexist results in the custom 404 page
Example 2: http://snavebelac.com/st-test blank page. This has an intentional 500 error within the custom 404 page.
I assume that because it is running through the custom 404.asp error page that this somehow blocks the custom 500 error page from functioning.
Does anyone know how I might be able to configure the sever so that the custom 404 page fires but 500 errors are output to the browser as they were in IIS6 OR is there a way to configure the server to process the custom 404 as well as the custom 500?
Thanks in advance.
You have to configure your web.config to handle 500 status errors, like this:
<system.web>
<customErrors mode="On" defaultRedirect="frmError.aspx">
<error statusCode="404" redirect="frmNotFound.aspx" />
<error statusCode="500" redirect="frmError.aspx" />
</customErrors>
</system.web>
Note the fourth line of code, where you say witch page has to be invoked since you got a 500 error.
Check out the solution posted here -
How can I properly handle 404 in ASP.NET MVC?
The key is -
Response.TrySkipIisCustomErrors = true;
I had a similar problem, the solution is really weird but works for sure.
I'll be very pragmatic, do the following.
<httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL" existingResponse="Auto">
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="500" subStatusCode="100" prefixLanguageFilePath="" path="/500.100.asp" responseMode="ExecuteURL" />
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/404.asp" responseMode="ExecuteURL" />
</httpErrors>
The blank page will be "not blank" if you assure that it ends with:
Response.Flush()
When IIS executes the code inside /500.100.asp it doesn't flush the response and it ends with a blank page.
I assure that "404" and "500.100" custom errors can be possible in IIS7.5/IIS8 and Classic ASP ;)
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.
I just want to see error insteas of asp.net showing me default error Server Error in '/' Application. What should i change in web config?
I used this,
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
Any suggestion i want to see errors in browser as i do live testing...
Change this:
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
To this:
<customErrors mode="Off" defaultRedirect="GenericErrorPage.htm">
RemoteOnly means you'll see the errors if you're logged onto the web server itself (e.g. http://localhost/) Off means to display the errors to everyone, including remote clients.
An alternative is to browse to your site using it's localhost url. This will allow you to see the errors, but with the customErrors = "RemoteOnly", users browsing to the site using a non-localhost url will see the friendly error page.
Also check the server's eventlog, if you're not confident that you are seeing the full error.
I have set up a page to handle errors that occur in a website I work on using web.config. I have used the customErrors section in web.config to do this, as described here
Here is the relevant section
<customErrors mode="On" defaultRedirect="page.aspx?IDDataTreeMenu=357">
<error statusCode="403" redirect="page.aspx?IDDataTreeMenu=357"/>
<error statusCode="404" redirect="page.aspx?IDDataTreeMenu=357" />
</customErrors>
This seems to work for all errors except 404. I just get the standard IIS 404 error when accessing a URL that doesn't exist.
What am I missing here?
This site is hosted on a shared server, so changing settings in IIS is not an option
Check with the ISP and see if they have a place where you can insert a reference to your own custom 404 page.
At Network Solutions they give you a control panel and a page where you can set this up.
<httpErrors>
<clear />
<error statusCode="404" subStatusCode="-1" path="/404.asp" responseMode="ExecuteURL" />
</httpErrors>