ASP.NET error handling for 404 pages without extension - asp.net

I have a website:Website Link with a problem, when it comes to extension less calls.
if you try something like:
This
I would like the user to go to:
here
but that is not what happens.
How can i change that?
Atm. i have this in my web.config:
<customErrors mode="On" defaultRedirect="/Error.aspx" redirectMode="ResponseRewrite">
<error statusCode="400" redirect="/Error.aspx?code=400" />
<error statusCode="403" redirect="/Error.aspx?code=403" />
<error statusCode="404" redirect="/Error.aspx?code=404" />
<error statusCode="500" redirect="/Error.aspx?code=500" />
</customErrors>
and
<system.webServer>
<httpErrors existingResponse="PassThrough" />
<modules runAllManagedModulesForAllRequests="true">
Some rewriting sends Error.aspx to "/findes-ikke"
After som more testing, with no luck, this is my entire web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation batch="false" targetFramework="4.7.1" debug="true" />
<customErrors mode="On" defaultRedirect="/Error.aspx" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="/Error.aspx?code=404" />
</customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="/findes-ikke?code=404" responseMode="ExecuteURL"/>
</httpErrors>
</system.webServer>
</configuration>

You can try the following:
change the web.config line to:
<error statusCode="404" redirect="/findes-ikke?code=404" />
You can also try to add following in web.config to redirect all http requests:
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="/findes-ikke?code=404" responseMode="ExecuteURL"/>
</httpErrors>
You can also do this to redirect all error requests. Make this change in the code you have given above:
<customErrors mode="On" defaultRedirect="/findes-ikke?code=404" redirectMode="ResponseRewrite">

Related

web.config not forwarding to 404 error page on non .aspx pages

Objective
I want all URLs of missing pages to forward to my 404 page which is in my root as 404error.aspx
Problem
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
Background
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.
What I already researched
404 Redirecting for non aspx pages - the answers there were incomplete or do not work.
404 does not append aspxerrorpath for non aspx pages - but that was a bit different than what I want.
I view many articles that I found on Google but nothing had a full example that actually worked.
Code
This is the entire code that I started with in my web.config
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
web.config (2)
<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
web.config (3)
<?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.
EDIT 1
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>
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>
The settings in the example customErrors section cause any unhandled HTTP 404 (file not found) errors to be directed to the Http404ErrorPage.aspx file. These HTTP 404 errors would occur if a request were made for an .aspx file, .asmx file, and so on and if the requested file did not exist. All other unhandled errors in ASP.NET files are directed to the DefaultRedirectErrorPage.aspx file.
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="true" />
<!-- Turn on Custom Errors -->
<customErrors mode="On"
defaultRedirect="DefaultRedirectErrorPage.aspx">
<error statusCode="404" redirect="Http404ErrorPage.aspx"/>
</customErrors>
</system.web>
</configuration>
NoteNote:
In the example, the mode attribute is set to "On" so that you can error messages when you run the example in Visual Studio. In a production environment, this setting would normally be "RemoteOnly". ASP.NET then renders error pages to external users. If a request is made on the server computer (localhost), ASP.NET renders a page with detailed error information.

Custom error redirect on 404 code for non-aspx files

I added to my web.config:
<customErrors mode="RemoteOnly" defaultRedirect="~/Pages/Error/Error.aspx">
<error statusCode="404" redirect="~/Pages/Error/Error404.aspx"/>
</customErrors>
and it works fine for .aspx pages.
When I try to type: http://domainname.com/notExist, I get the default 404.
I tried the suggestions from:
404 Redirecting for non aspx pages
but nothing works for me.
Any ideas why?
I use .net 3.5, application pool pipeline mode is Integrated.
After adding <httpErrors> I get:
Try This, it will provide separate status. Then you can execute as per your requirement.
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="~/Pages/Error/Error.aspx">
<error statusCode="404" redirect="~/Pages/Error/Error404.aspx"/>
</customErrors>
</system.web>
<system.webserver>
<asp scriptErrorSentToBrowser="true"/>
<httpErrors errorMode="Detailed">
<error statusCode="401" path="/Pages/Error/Error.aspx" responseMode="Redirect" />
<error statusCode="403" path="/Pages/Error/Error.aspx" responseMode="ExecuteURL" />
<error statusCode="404" path="/Pages/Error/Error404.aspx" responseMode="ExecuteURL" />
<error statusCode="500" path="/Pages/Error/Error.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webserver>
You need to separate setting for other pages.
Try something like this.
<system.webserver>
<httpErrors errorMode="Custom" >
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="~/Pages/Error/Error404.aspx" />
</httpErrors>
</system.webserver>

Error in web.config redirect to error404 page

Getting internal server error... want to redirect page on error "404 Page not found".
<?xml version="1.0"?>
<configuration>
<system.web>
<urlMappings enabled="true">
<add url="~/Error404" mappedUrl="~/Error404.aspx"/>
</urlMappings>
<pages controlRenderingCompatibilityVersion="4.0"/>
<customErrors mode="on" defaultRedirect="~/Error404">
<error statusCode="404" redirect="~/Error404" />
<error statusCode="500" redirect="~/Error404" />
</customErrors>
</system.web>
<system.webServer>
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/Error404" responseMode="ExecuteURL"/>
</httpErrors>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
Check this out:
Implementing a Custom Error page on an ASP.Net website
Also, it looks like you're missing ".aspx" at the end of your page paths.

Example of Configuration for <httpErrors>

I have a Web Application in Asp.Net 4 running locally on IIS 7.
I need display a Custom Page (404) and an 500 instead for the defaults page for IIS.
Using this httpErrors in Web.Config
<system.webServer>
<httpErrors>
My Site is in
C:\inetpub\wwwroot\mysite\
My Custom Error page in:
C:\inetpub\wwwroot\mysite\ErrorPages\404.htm
C:\inetpub\wwwroot\mysite\ErrorPages\505.htm
I do not understand how it works. COuld you please provide me a sample of code?
Thanks
I solved my problem with this.
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode='-1' />
<remove statusCode="500" subStatusCode='-1' />
<error statusCode="404" path="/404.aspx" prefixLanguageFilePath="" responseMode="ExecuteURL" />
<error statusCode="500" path="/500.aspx" prefixLanguageFilePath="" responseMode="ExecuteURL" />
</httpErrors>
This needs to go in Web.config, under <configuration> > <system.webServer>
e.g.
<configuration>
<system.webServer>
<httpErrors ...>
// define errors in here ...
</httpErrors>
</system.webServer>
</configuration>
Here is an example, hope it helps
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="default.aspx">
<error statusCode="404" redirect="~/ErrorPages/404.htm"/>
<error statusCode="500" redirect="~/ErrorPages/505.htm"/>
</customErrors>
</system.web>
Edit for comments: Here's the example I think you need
<configuration>
<system.webServer>
<httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="File" >
<remove statusCode="500" />
<error statusCode="500"
prefixLanguageFilePath="C:\Contoso\Content\errors"
path="500.htm" />
</httpErrors>
</system.webServer>
</configuration>
http://www.iis.net/ConfigReference/system.webServer/httpErrors/error

Asp.net - Web.Config - Custom Errors

How can I set 404 and other error pages using web.config? I have tried adding following block in web.config.
<customErrors defaultRedirect="Forms/Errors/Page_404.aspx" mode="On">
<error statusCode="500" redirect="servererror.aspx" />
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="Forms/Errors/Page_404.aspx" />
</customErrors>
but still its showing default error page of IIS7. How to fix this?
I solved it myself. We need to add another section in web.config like below to make it work in IIS 7 / 7.5. For IIS 6 the one works which I mentioned in my question
<system.webServer>
...
<httpErrors errorMode="Custom" >
<remove statusCode="403" subStatusCode="-1" />
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="404" path="/404.aspx" responseMode="Redirect" />
<error statusCode="403" path="/403.aspx" responseMode="Redirect" />
<error statusCode="500" path="/500.aspx" responseMode="Redirect" />
</httpErrors>
...
</system.webServer>
Thanks to everyone who answered.
Try putting this in the system.webServer section of your Web.config
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
Try to add the "~/" before paths:
<customErrors defaultRedirect="~/Forms/Errors/Page_404.aspx" mode="On">
<error statusCode="500" redirect="~/servererror.aspx" />
<error statusCode="403" redirect="~/NoAccess.htm" />
<error statusCode="404" redirect="~/Forms/Errors/Page_404.aspx" />
</customErrors>
It looks like you're using a relative path there. Could that be the problem?
Try using Fiddler to see what page your browser is being redirected to.

Resources