HttpErrors Custom Error Page - asp.net

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" />

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

ASP.NET MVC custom error pages stuck in a redirection loop

I'm trying to show custom error pages in my ASP.NET MVC app.
I understand that some errors are handles by ASP.NET and others by IIS.
The ASP.NET ones are handled and work fine
<customErrors mode="On" defaultRedirect="~/Error/Index"/>
When I request a URL that does not exist and references a static page like .html, I expect IIS to handle it.
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="http://localhost/MySite/404.html" responseMode="Redirect"/>
</httpErrors>
And that works. However I don't want to hard code http://localhost in there but make it a relative path.
So I tried
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="~/404.html" responseMode="Redirect"/>
</httpErrors>
However that keeps redirecting me (it appears) as the URL ends up being http://localhost/MySite/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/~/404.html
It also works if I do
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="404.html" responseMode="Redirect"/>
</httpErrors>
But of course only if the error is in http://localhost/MySite/doesnotexist.html root directory but not in any other like http://localhost/MySite/somedir/doesnotexist.html
If I change the responseMode to any of the other options it doesn't work at all, shows me the default IIS 404 page.
I'm deeply puzzled, what is causing the redirection loop?
I've been able to replicate the behaviour you are describing. Mine was fixed when I changed the responseMode to file. I.e. like this
<httpErrors errorMode="Custom">
<remove statusCode="404" />
<error statusCode="404" path="404.html" responseMode="File" />
</httpErrors>
But maybe the problem is because you have put your 404.html in the mysite folder. Assuming it has to stay there I think it should work if you do this
<httpErrors errorMode="Custom">
<remove statusCode="404" />
<error statusCode="404" path="/mysite/404.html" responseMode="Redirect" />
</httpErrors>
The path attribute specifies the file path or URL that is served in response to the HTTP error specified by the statusCode and subStatusCode attributes. If you choose the File response mode, you specify the path of the custom error page. If you choose the ExecuteURL response mode, the path has to be a server relative URL (for example, /404.htm). If you choose the Redirect response mode, you have to enter an absolute URL (for example, www.contoso.com/404.htm).
The responseMode attribute specifies how custom error content is returned. The responseMode attribute can be one of the following possible values. 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.

Custom Error pages in MVC 5

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.

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