Custom error pages with HandleError attribute - asp.net-mvc

I have asp.net web application and try to specify custom HTML for Dynamic Ip Restrictions module, which returns 401 prior to .net code.
I've added following to my web.config:
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="401" subStatusCode="-1" />
<error statusCode="401" path="ddos.html" responseMode="File" />
<error statusCode="401" subStatusCode="502" path="ddos.html" responseMode="File" />
<error statusCode="404" prefixLanguageFilePath="" path="/404" responseMode="ExecuteURL" />
</httpErrors>
Productin environment (windows Server 2008): Pages with 404 and 401 works fine, but exception handling is broken - i see built-in iis "red" page instead of custom page.
Development environment (windows 7/8): 404 and erors are fine, but 401 redirect user to login page.
Exception handling defined as:
[HandleError(View = "Error", Master = "~/Views/Shared/_ThreeColumnsLayout.cshtml")]
UPD1: Also, I can add that if I remove pages with code 500, I see foloowing plain text message:
The page cannot be displayed because an internal server error has occurred.

<httpErrors existingResponse="Auto">
does the trick

Related

Error returning html response instead of error page

I've setup a custom error.html page and when a user tries to upload a large file it throws an error due to max request length (expected behaviour).
However, instead of returning a rendered page it returns the html of the error page and the network shows a 200 response instead of 500.
Here is what I have in the web config
<system.web>
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/error.html">
<error statusCode="404" redirect="~/error.html" />
<error statusCode="500" redirect="~/error.html" />
</customErrors>
</system.web>
and under the webServer section
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404" />
<error statusCode="404" path="error.html" responseMode="File" />
<remove statusCode="500" />
<error statusCode="500" path="error.html" responseMode="File" />
</httpErrors>
</system.webServer>
It could be that these sections maybe conflicting with each other.
The expected error page is displayed for a 404.
(Increasing the limit is possible but won't stop the issue happening from a different error)

configuration custom errors gives "error too many redirects" in mvc

I configured my config file for custom errors in MVC. So the custom error configurations are:
<customErrors mode="On">
<error statusCode="401" redirect="~/Views/UnAuthorized/ErrorUnauthorised"/>
<error statusCode="404" redirect="~/Views/UnAuthorized/ErrorResourceNotFound"/>
</customErrors>
<authentication mode="Windows" />
<authorization>
<deny users="?"></deny>
<allow users="*"></allow>
</authorization>
and for the server
<httpErrors errorMode="Custom" >
<remove statusCode="401" />
<error statusCode="401" path="http://localhost:port/UnAuthorized/ErrorUnauthorised" responseMode="Redirect" />
<remove statusCode="404"/>
<error statusCode="404" path="http://localhost:port/UnAuthorized/ErrorResourceNotFound" responseMode="Redirect" />
</httpErrors>
My Error pages are in different folder with other config file which allows unauthenticated users with:
<allow users="*"></allow>
My Error pages controller is:
public class UnAuthorizedController : Controller
{
// GET: UnAuthorized
public ActionResult ErrorUnauthorised()
{
return View("ErrorUnauthorised");
}
public ActionResult ErrorResourceNotFound()
{
return View("ErrorResourceNotFound");
}
}
When I try to start application it starts with this page ~/Views/UnAuthorized/ErrorUnauthorised and shows browser message "Too many redirects" When I remove httpErrors section the application starts normally with Index page, but on error it doesn't shows the custom error pages
EDIT
I ended up with conclusion that application shows error "too many redirects" if I have both 401 and 404 errors in my config file. If I remove the 401 error the application is working fine
You dont need the httpErrors tag. Just add the following under system.web
<customErrors mode="On" defaultRedirect="errorpage.htm?error=1" >
<error statusCode="404" redirect="filenotfound.htm?error=1" />
<error statusCode="401" redirect="notauthorized.htm?error=1" />
</customErrors>
notes:
1- I add the '?error=1' query string to inform asp.net that i dont want the aspxerrorpath query string to be added when an error occurs.
2- Put your .htm files at the application root folder.
3- If you have some messages you need to generate with c# code and display them inside the error pages then use the application_error event in global.asax to catch the relevant error and redirect the user to desired page.

Remove all IIS error messages via <httpErrors> in web.config

I'm implementing the following:
<httpErrors errorMode="Custom">
<remove statusCode="404" />
<error statusCode="404" path="~/Error" responseMode="ExecuteURL" prefixLanguageFilePath="" />
<remove statusCode="500" />
<error statusCode="500" path="~/Error" responseMode="ExecuteURL" prefixLanguageFilePath="" />
</httpErrors>
However I'd like to remove ALL IIS error pages so that none of my internal details are exposed on 404, 403, 500 etc. I've tried to use:
<remove statusCode="*" />
To no avail. Is there a standard solution here?
As #Peter said, you can use the <clear> element.
You can use the <remove> element to remove a specific error message from the collection of error messages your site or application inherits from a higher level in the IIS configuration hierarchy. Also, you can use the <clear> element to remove all HTTP error messages from the collection of HTTP error messages that your site or application inherits.
Reference: iis.net
Example:
<httpErrors errorMode="Custom">
<clear />
<error statusCode="404" path="/Error" responseMode="ExecuteURL" />
<error statusCode="500" path="/Error" responseMode="ExecuteURL" />
</httpErrors>
(Side note: Virtual paths are not allowed in the path attribute of the error element.)

Elmah not logging 404 errors after third folder, and 403 errors at all

Continuing my previous question, I handle custom errors in my mvc5 app using:
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<error statusCode="404" path="/Errors/NotFound" responseMode="ExecuteURL" />
<remove statusCode="403"/>
<error statusCode="403" path="/Errors/NotFound" responseMode="ExecuteURL" />
<remove statusCode="500"/>
<error statusCode="500" path="/Errors/ServerError" responseMode="ExecuteURL" />
</httpErrors>
...
</system.webServer>
This enabled me to show custom 404 error page after the 3rd folder (e.g. site.com/a/b/c/nothing-here). Yet, Elmah is not logging these errors. This is also true for 403 errors. So My questions are:
is there a way to make Elmah handle all server errors regardless of
being thrown by the mvc app itself?
if not, since /Errors/NotFound is successfully executed, I can add a log from that action. is there a way to tell if Elmah already handled this error (or was it thrown by the mvc app)?

How to set Error page in webconfig for mvc web based system

I have MVC 4 web based system. I need to handle error page in web.config file. I m try do with httpError syntax. When 404 it will show blank page. not show the actual error page. url also not show as error page.
web.config code:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error statusCode="404" responseMode="ExecuteURL" path="~/Error/PageNotFound.htm" />
</httpErrors>
please help this.
Try looking at this page: Error Handling and Nice Error pages.
It says to add the following in the Web.Config:
<customErrors mode="On" defaultRedirect="~/Content/Errors/page500.aspx" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="~/Content/Errors/page404.aspx" />
</customErrors>
I haven't got my MVC code infront of me but I think this is what I did.

Resources