How to show custom error page instead of iis 403 error page? - iis

my web.config :
<customErrors mode="Off" redirectMode="ResponseRewrite" defaultRedirect="~/Pages/Error/DefaultError.aspx">
<error statusCode="404" redirect="~/Pages/Error/Page404.aspx" />
<error statusCode="500" redirect="~/Pages/Error/DefaultError.aspx" />
</customErrors>
<httpErrors existingResponse="Replace" defaultResponseMode="ExecuteURL" errorMode="Custom"> //also used other options for existingResponse
<remove statusCode="403"/>
<error statusCode="403" path="~/Pages/Error/PI403.aspx" responseMode="ExecuteURL"/>
</httpErrors>
and here is my Application_error method in the global.asax.cs file:
Server.ClearError(); //clear the error so we can continue onwards
var errorPage = "~/Pages/Error/DefaultError.aspx";
var httpException = ex as HttpException;
if (httpException != null && httpException.GetHttpCode() == (int)HttpStatusCode.NotFound)
{
errorPage = "~/Pages/Error/Page404.aspx";
}
Response.Redirect(errorPage);
I have the Logs file in the project but this file uses just logging. If I use myURL/Logs browser link, I get IIS 403.14 error page. If I write myURL/asdeds, I get my custom error page (does not existing something like that in my project). Because the 403 exception does not trigger Application_Error. I want to show my custom error page for all exceptions. I should see my custom error page when I write myURL/Logs to URL part.
and also I set TrySkipIisCustomError property in the Application_BeginRequest
HttpContext.Current.Response.TrySkipIisCustomErrors = true;

Since the 403.14 - Forbidden error is the IIS error not the asp.net error. It use the StaticFile http handler not the asp.net http handler. So the Application_error will not be fired when you faced this error.
The only way to modify the custom error page is using IIS custom error page.
As Lex says, the IIS custom error page not support "~". It will auto match your web application root path. So you could use below config setting.
<httpErrors existingResponse="Replace" defaultResponseMode="ExecuteURL" errorMode="Custom"> //also used other options for existingResponse
<remove statusCode="403"/>
<error statusCode="403" path="/Pages/Error/PI403.aspx" responseMode="ExecuteURL"/>
</httpErrors>
Also you could open IIS mangement console and use UI window to modify the setting.

First you must go to IIS
select configuration editor like this:
In the page that opens, open section combo box and select httpErrors
In the page that opens, unlock defaultPath
Repeat steps 1, 2 and 3 for your application on IIS
Go to Error Pages of your application
Select 403 error and edit it. in the page of edit action you can select Respond with a 302 redirect and type custom error page address in Absolute URL text box.
Do not forget, this operation is performed on the server. Now if a request sent to the server (smp:https :// yoursite.com/scripts) And if you get error 302, the page you want will open.

Related

404 Custom errors not shown for aspx extensions

I created a 404.html page and set it in Error Documents. It works well for everything except aspx pages. How can I redirect those pages also?
Thanks
As far as I know, if you want to show custom 404 error in asp.net web form application, you should use the asp.net custom error page not IIS error page.
About how to set it ,you could refer to below config.
<system.web>
<customErrors mode="On" >
<error statusCode="404" redirect="error.html"/>
</customErrors>
</system.web>
Notice: The error.html should put in your asp.net web applicaton's root path.
More details,you could refer to below article:
https://learn.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/deploying-web-site-projects/displaying-a-custom-error-page-cs

404 errors not going to custom page

Thanks in advance for replying.
Simple Azure ASP.net 4.5 web forms website.
I am handling custom errors in the Global.asax. Page missing works when the middle of the url is changed. However, if the end is changed. It says 404 error and when deployed to Azure, it states a single line "Resource missing"
I am thinking this is web server issue and if my huntch is correct, how do I
a) Fix this on the IIS express in VS 2013
b) Fix this when deployed to Azure Websites
Cheers, Thnx & All the best
KFC
First of all, I'm kind of shooting in the dark here. Could you possibly post the httpErrors and customErrors nodes from your Web.config, and the error handling code you're using in Global.asax?
That said, Global.asax isn't a great place to be handling custom error pages. You can use the httpErrors node in your Web.config to have IIS (7+, Azure is fine) catch errors and redirect. This lets you catch errors from inside (e.g. application exceptions) and outside (e.g. request validation failures, missing static files) your application:-
<httpErrors errorMode="Custom" existingResponse="Replace">
<clear />
<error statusCode="400" responseMode="ExecuteURL" path="/Error" />
<error statusCode="403" responseMode="ExecuteURL" path="/Error/Forbidden" />
<error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
<error statusCode="500" responseMode="ExecuteURL" path="/Error" />
</httpErrors>
Remove the customErrors node entirely. It is unnecessary.
I've put a full working example of using httpErrors for custom error pages up on github. You can also see it live on azure web sites.

Custom errors not working with IISExpress

I have a asp.net mvc application and am trying to get custom errors working with IISExpress.
Works in Casini fine:
<customErrors mode="On" defaultRedirect="/error">
<error statusCode="404" redirect="/error/notfound"/>
</customErrors>
When I've deployed mvc sites to IIS (7.5) before, all I had to do get my custom errors working was to set:
<httpErrors errorMode="Detailed"/>
I've tried explicitly specifying the status codes within the httpErrors section but nothing works. Here's an example:
<httpErrors errorMode="Detailed" defaultResponseMode="Redirect">
<clear/>
<error statusCode="404" path="/error/notfound"/>
</httpErrors>
Any ideas?
Thanks
Ben
This was caused partly due to my misunderstanding of how custom errors are actually invoked and also the fact that (IMHO), the handling of errors in asp.net mvc is a bit messed up.
The first issue was that in a number of my action methods, I was checking for the existence of an object e.g. a blog post, and returning a HttpNotFoundResult if the blog post was null. I was under the assumption that this would then display the custom error page that I had set up for 404 errors.
However, this is not the case. Returning a HttpNotFoundResult simply sets the status code of the response to 404. The rest is then handled by IIS, displaying the IIS 404 error page or by your browser if it has it's own custom error page.
One solution here is to return a HttpException which will use your custom error pages since the request is be handled by asp.net.
I chose instead to create a new ActionResult that allowed me to specify a view along with a http status code. I preferred this to throwing exceptions.
The next issue was that by default a new MVC project has a greedy route defined. If you make a request to /foo/bar the default MvcHandler will look for a controller called Foo. When it can't find it, it will return 404.
I had removed the default route and had no greedy routes. This meant that urls not matching any of my routes would not be handled by asp.net and would just fall back to IIS.
The solution here was to create a wildcard route at the bottom of my routing configuration to match all other requests and forward them to a custom PageNotFound action, that sets the status code to 404 and displays my custom view.
Some things worth pointing out.
You will need to set httpErrors errorMode="Detailed" for your custom error pages to be displayed in IIS/IISExpress. The rest however can be left alone.
Setting the defaultRedirect path in the customErrors section has no effect on 500 errors. This is because the global HandleErrorAttribute handles all 500 errors and just looks for a view called "Error" to display. This means that if your custom error page is actually a controller action, it will not be invoked. The above is true even if you explicitly specify a 500 error page.
You should still keep the defaultRedirect path however, as it will be used for other status codes if they are not specified explicitly.
If you are using iisexpress you can just comment out the entire httpErrors section < !-- --> in the applicationhost.config and replace it with the following:
<httpErrors errorMode="Custom">
<error responseMode="Redirect" statusCode="404" path="../missing/index.php" />
</httpErrors>
path is the url path to your custom site specific page

IIS7 Hijacks My Coldfusion Error Page

In my exception handling file, I set a statuscode to 404 and then render n HTML page, for the error page (think fail-whale).
<cfheader statuscode="404" statustext="Application Exception">
<html><head><title>Error</title></head><body><h1>There was an error yo!</h1></body></html>
This is obviously over simplified, but just to make sure everything was demonstrated.
What I have found is that from a ASP.NET request, they can set a variable "Response.TrySkipIisCustomErrors=true" to keep IIS from showing its own error page.
How can someone in Coldfusion do it / how can I just tell IIS to stop its thinks it knows better than me shenanigans.
This might help:
<configuration>
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
</configuration>
For more information:
HTTP Errors (IIS.NET)
What to expect from IIS7 custom error module (IIS.NET)
If that doesn't work then you might try writing a .NET HttpModule to plug into the IIS request/response pipeline to set Response.TrySkipCustomErrors. Not ideal.
ASP.NET's worker request object calls an exported function called MgdSetStatusW. The problem here is that unless Coldfusion exposes this flag then you won't be able to set the value directly in CF.
Poking around with .NET Reflector I seen ASP.NET setting the response status using:
[DllImport("webengine4.dll", CharSet=CharSet.Unicode)]
internal static extern int MgdSetStatusW(IntPtr pRequestContext,
int dwStatusCode, int dwSubStatusCode, string pszReason,
string pszErrorDescription, bool fTrySkipCustomErrors);

404 page in Umbraco?

I installed Umbraco 4.5 and it is running fine. one thing i cant get to work though, is the 404. When it hit a page that does not excist it shows the default IIS7 404 page, and not the built-in umbraco 404 page.
So i am asuming it is a setting in the iis i have to change - but which?
Copy from http://our.umbraco.org/forum/using/ui-questions/8244-IIS7--404:
Basically, you need to add
<location path="Site Description">
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
</location>
to your applicationHost.config file where "Site Description" is the name of your site in IIS7.
The applicationHost.config file is located in: system32\inetsrv\config
Edit:
As stated in the comments if this answer, you should add this section in your web.config instead which is way better, you should always avoid altering config files outside your own application that may affect other applications.
in config/umbraco.settings you can set the umbraco page to load for custom 404
<errors>
<!-- the id of the page that should be shown if the page is not found -->
<!-- <errorPage culture="default">1</errorPage>-->
<!-- <errorPage culture="en-US">200</errorPage>-->
<error404>1296`</error404>`
</errors>
The page error page ID goes between the <error404> & </error404> tags.

Resources