I want all URLs of missing pages to forward to my 404 page which is in my root as 404error.aspx
So far only the URLs that have a .aspx
will work. For example, if you enter 4error.aspx
you will be redirected to the error page.
/404error.aspx?aspxerrorpath=/4error.aspx
I am not a .NET developer, so I just took over this project which uses classic .aspx
. So I am not using any Microsoft product to build any of this in templates or frameworks. I am just coding in Sublime text.
I view many articles that I found on Google but nothing had a full example that actually worked.
This is the entire code that I started with in my web.config
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="~/404error.aspx" />
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-US"
uiCulture="de-DE"
/>
</system.web>
</configuration>
I have also found this example from Microsoft (404error.aspx was my modification)
http://msdn.microsoft.com/en-us/library/vstudio/bb397417%28v=vs.100%29.aspx
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="true" />
<!-- Turn on Custom Errors -->
<customErrors mode="On"
defaultRedirect="/404error.aspx">
<error statusCode="404" redirect="/404Error.aspx"/>
</customErrors>
</system.web>
</configuration>
That also did not handle pages that did not have the .aspx
Then I tried this example from
<?xml version="1.0"?>
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="~/404error.aspx" />
<error statusCode="404" redirect="~/404error.aspx" />
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-US"
uiCulture="de-DE"
/>
</system.web>
<system.webServer>
<httpErrors>
<remove statusCode="401" subStatusCode="-1" />
<remove statusCode="403" subStatusCode="-1" />
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<!-- full url when responsemode is Redirect -->
<error statusCode="401" path="http://foo.com/default.htm" responseMode="Redirect" />
<!-- local relative path when responsemode is ExecuteURL -->
<error statusCode="403" path="~/404error.aspx" responseMode="ExecuteURL" />
<error statusCode="404" path="~/404error.aspx" responseMode="ExecuteURL" />
<error statusCode="500" path="~/404error.aspx" responseMode="ExecuteURL" />
</httpErrors>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Obviously I have to change the paths for other-than-404 pages but I just wanted to test it for those errors. That last web.config
works even worse.
Can you please tell me what I need to modify. I would appreciate a full <configuration>
because I keep getting confused what goes where.
I had read that many developers were suggesting to just make it a HTML page. So now my page is 404.html
Here is my updated web.config
<configuration>
<system.web>
<customErrors mode="On"
redirectMode="ResponseRewrite">
<error statusCode="404"
redirect="~/404.html"/>
</customErrors>
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-US"
uiCulture="de-DE"
/>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom"
defaultResponseMode="File">
<remove statusCode="404"/>
<error statusCode="404"
path="~/404.html"/>
</httpErrors>
</system.webServer>
</configuration>
If you want to set your application level exception should redirect to your custom error page, you can do this by going to global. asax file and write the code for redirection in Application_Error method. For handling all the exception, you just need to write the code for redirection at catch section.
You need to configure the <httpErrors>
element. This configures the error pages for both static files and server pages.
Your 3rd attempt "web.config (3)" and "Edit 1" are almost there. The problem is that you can't use app-relative paths here (ex: "~/404.html"), they have to be relative from the site root (ex: "/404.html").
<?xml version="1.0"?>
<configuration>
<system.webServer>
<httpErrors>
<remove statusCode="401" subStatusCode="-1" />
<remove statusCode="403" subStatusCode="-1" />
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<!-- full url when responsemode is Redirect -->
<error statusCode="401" path="http://foo.com/default.htm" responseMode="Redirect" />
<!-- local relative path when responsemode is ExecuteURL -->
<error statusCode="403" path="/404error.aspx" responseMode="ExecuteURL" />
<error statusCode="404" path="/404error.aspx" responseMode="ExecuteURL" />
<error statusCode="500" path="/404error.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
</configuration>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With