Custom Error pages in MVC 5 - asp.net

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.

Related

httpErrors not working in case of angular app deployed using Azure Web App

I have a angular app hosted using Azure Web app. I want to display custom error pages for respective HTTP Status codes : 400, 403, 404, 500
Here is the app URL: https://test.com
Now in case anyone navigates to a non-existing path say for example : https://test.com/**/nonexistingpath/test , in that case I want to display a custom error page instead of the default error page.
I introduced the following code snippet in the web.config file :
<httpErrors errorMode="Custom" existingResponse="Auto" defaultResponseMode="File" >
<remove statusCode="400" subStatusCode="-1"/>
<error statusCode="400" path="/public/400.html" />
</httpErrors>
Now on navigating to the non-existing path: https://test.com/**/nonexistingpath/test
I am not seeing the custom error page and instead of that I still see the default server error page which describes to set CustomError mode to Off
I am not sure why httpErrors are not picked up in this case.
Now I added the below snippet:
<customErrors mode="On" defaultRedirect="/public" >
<error statusCode="400" redirect="/public/400.html" />
</customErrors>
Again I navigated to the location : https://test.com/**/nonexistingpath/test
I see the URL in the browser : https://test.com/public/400.html?aspxerrorpath=/**/nonexistingpath/test
In this case I don't want to see the querystring value :aspxerrorpath=/**/nonexistingpath/test after the ? symbol
Can anyone help me with their guidance to fix this issue?
I did the following steps to fix the issue:
Removed the customErrors section completely
Added the below code :
<httpErrors errorMode="Custom" existingResponse="Replace" defaultResponseMode="ExecuteURL" >
<remove statusCode="400" subStatusCode="-1"/>
<error statusCode="400" path="/public/400.html" responseMode="ExecuteURL"/>
</httpErrors>
I validated the above changes and found working fine :)

HttpErrors Custom Error Page

I am trying to use in a asp.net webforms application custom error pages so i am doing something like this (example the 404 error) :
<httpErrors errorMode="Custom" existingResponse="Replace" defaultResponseMode="ExecuteURL">
<clear />
<error statusCode="404" path="/Pages/Errors/Error404.aspx" responseMode="ExecuteURL" />
</httpErrors>
now for the problem, it seems to work half-way, meaning that when a not found page error occurs it is trying to redirect to my Error404.aspx page but resulting in another 404 error because it can't find my custom page . This is INCEPTION!!
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Pages/Errors/Error404.aspx
i checked and rechecked my path ,Pages and Errors folders exists, Error404.aspx exists, if i write the path in browser i can access the page , so indeed exists,
so what is the problem?
Try to add the ~ in the path:
<error statusCode="404" path="~/Pages/Errors/Error404.aspx" responseMode="ExecuteURL" />
another solution is using redirect:
<error statusCode="404" redirect="~/Pages/Errors/Error404.aspx" />

Custom 404 page errors for .aspx extensions

I have been trying to have my web app to redirect to a custom 404 page. It works for all urls except if they have a ".aspx" extension
The server is a Windows Server 2008 and here are the following settings I have in my web.config (using google.com as a quick example):
<customErrors defaultRedirect="http://www.google.com" mode="On" redirectMode="ResponseRedirect"></customErrors>
<httpErrors errorMode="Custom">
<clear />
<remove statusCode="500" subStatusCode="-1" />
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/404-Page/" responseMode="ExecuteURL" />
<error statusCode="500" prefixLanguageFilePath="" path="/404-Page/" responseMode="ExecuteURL" />
</httpErrors>
Again the HTTP Errors work for everything but extensions of ".aspx"
The customErrors element provides information about custom error messages for an ASP.NET application. Try adding a error child element to the customErrors element for the specific HTTP error code you want to trap.
<error statusCode="404" redirect="error404.htm"/>
I had a similar problem in IIS6. I ended up handling it in Application_Error in global.asax. To make it work I had to set the 404 custom page to an aspx page that didn't exist (if I set it to an existing aspx page it would get swallowed up by EPiServer's internal error handling...)
Well to solve this issue, we ended up having to create a module that hijacks any errors and transfers the user to my custom 404 page that is set in the web.config (under customErrors). The module would add an event handler to whenever the application gets an error:
public void Init(HttpApplication context)
{
context.Error += new EventHandler(FileNotFound_Error);
}
And the function FileNoteFound_Error does the redirection.

Finding Classic ASP errors in a custom 404 error page in IIS7

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 ;)

HttpError will not show custom error pages

I've got this in the web.config:
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/Error/NotFound.aspx" responseMode="Redirect" />
<error statusCode="500" prefixLanguageFilePath="" path="/Error/ServerError.aspx" responseMode="Redirect" />
</httpErrors>
But IIS still shows the built in error page.
Any ideas?
You may also need to set the existingReponse attribute in the httpErrors element like this:
<httpErrors errorMode="Custom" existingResponse="Replace">
<clear />
<error statusCode="404" prefixLanguageFilePath="" path="/ErrorHandler.aspx" responseMode="ExecuteURL" />
<error statusCode="500" prefixLanguageFilePath="" path="/ErrorHandler.aspx" responseMode="ExecuteURL" />
</httpErrors>
This is how I am using it and it works to me, it looks pretty similar except for the subStatusCode directives and the ExecuteURL.
<httpErrors>
<!--Remove inherited 500 error page setting -->
<remove statusCode='500' subStatusCode='-1'/>
<!--Override the inherited 500 error page setting with the 'My500.html' as its path-->
<error statusCode='500' subStatusCode='-1' prefixLanguageFilePath='' path='/My500.html' responseMode='ExecuteURL'/>
</httpErrors>
If you are using ExecuteURL, the custom error page path must be in the same application pool as the application itself.
For architectural reasons, IIS 7.0 can only execute the URL if it is located in the same Application Pool. Use the redirect feature to execute a Custom Error in a different Application Pool.
It seems as though you are using a server relative URL, try setting responseMode="ExecuteURL", from MSDN.
ExecuteURL
Serves dynamic content (for example,
an .asp file) specified in the path
attribute for the custom error. If
responseMode is set to ExecuteURL, the
path value has to be a server relative
URL. The numeric value is 1.
Redirect
Redirects client browsers to a the URL
specified in the path attribute that
contains the custom error file. If
responseMode is set to Redirect, the
path value has to be an absolute URL.
The numeric value is 2.
Ensure that you have the proper feature setting for the Error Page redirection in IIS. To check this, from the Error Pages page in IIS Manager, click Edit Feature Settings and make sure Custom error pages is checked if you are testing the redirects from the web server itself. If you are testing remotely, you can leave Detailed errors for local requests and custom error pages for remote requests is checked. This appears to be the default option in my test environment.

Resources