This question already exists:
Closed 10 years ago.
Possible Duplicate:
iis 7.5 hijacking 404
I'm running a .net 4.0 site over a IIS 7.5.
I have added the custom errors section to my webconfig:
<system.web>
<customErrors defaultRedirect="/error" mode="On">
<error statusCode="404" redirect="/error?code=404"/>
<error statusCode="500" redirect="/error?code=500"/>
</customErrors>
</system.web>
The custom errors are working. Any internal server errors are redirected to /error?code=500 and any throw new HttpException(404, "Not found"), when a missing parameter is found, are redirected to /error?code=404. But any non existing url path still throws the standard IIS 404 page, not my custom 404.
Any tip?
That happened to me once in IIS 7, the solution was to configure the IIS-Application error pages:
Open IIS
Select your applciaiton
Open Error Pages under the IIS section
In the right panel named Actions, click Edit Feature Settings
To test choose:
Custom Error Pages
Accept the changes
Restart IIS
That did the trick in my case
Edit 1
Remember that the customErrors section works only for requests handled by ASP.Net the static requests like html pages will still redirect to the 404 error page configured in IIS
However, you could override this behavior in your own config file:
For more info
<configuration>
<system.webServer>
<httpErrors existingResponse="Replace" errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="GenericError.aspx" responseMode="Redirect" />
</httpErrors>
</system.webServer>
</configuration>
You also might need to mark the above section as updatable:
Open the file:
C:\Windows\System32\inetsrv\config\applicationHost.config
And change:
<section name="httpErrors" overrideModeDefault="Deny" />
To:
<section name="httpErrors" overrideModeDefault="Allow" />
You can handle it in Global.asax or in IHttpModule.
Exception currentException = Server.GetLastError();
HttpException httpException = currentException as HttpException;
if (httpException != null && httpException.GetHttpCode() == 404)
{
// redirect
}
Check http://learn.iis.net/page.aspx/508/wildcard-script-mapping-and-iis-integrated-pipeline/ for info how to setup wildcard mapping, so all requests are handled by asp.net.
Related
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.
I have a web application in which I applied URL rewriting .I have also applied custom 404 error page with web.config settings and also with global.asax. Both of them working locally,but not on live server.Is there a GoDaddy issue where site has deployed?
Web.config code
<customErrors mode="On" defaultRedirect="" >
<error statusCode="404" redirect="~/ErrorPages/404.aspx" />
</customErrors>
Global.asax code
Exception ex = Server.GetLastError();
if (ex is HttpException)
{
if (((HttpException)(ex)).GetHttpCode() == 404)
{
Server.ClearError();
Server.Transfer("~/ErrorPages/404.aspx");
}
}
I have also tried for following
<system.webServer>
<httpErrorserrorMode=“Custom“ >
<removestatusCode=“404“ subStatusCode=“-1“/>
<errorstatusCode=“404“ path=“~/ErrorPages/404.aspx“ responseMode=“ExecuteURL“ />
</httpErrors>
</system.webServer>
I have also tried setting custom 404 page in godaddy but no result.What will be the solution of this problem?Thanks in advance.
Tried for setting custom 404 page in godaddy's control panel and waited for some time.Then its working for me.
I need help with settings. It's never use provided page for error 404.
This is my customErrors web.config block:
<customErrors mode="On" defaultRedirect="/systemerror.aspx">
<error statusCode="404" redirect="/404.aspx" />
</customErrors>
I have these page created and they works well if I call them by url, but when I write in url non-existent page like:http://www.mysite.com/asdfasdasd I am getting:
Server Error
404 - File or directory not found.
The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.
It's not use my custom error page 404.aspx, how can I fix this ?
If you are using IIS 7, try:
Response.TrySkipIisCustomErrors = true;
Refer:
HttpResponse.TrySkipIisCustomErrors Property :Gets or sets a value that specifies whether IIS 7.0 custom errors are disabled.
You are missing ~
<customErrors mode="On" defaultRedirect="~/systemerror.aspx">
<error statusCode="404" redirect="~/404.aspx" />
</customErrors>
This should work. Let me know.
In my ASP.NET 3.5 Website which is published in shared hosting provider , I've configured my web.config file like this :
<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="AccessDenied.htm"/>
<error statusCode="404" redirect="FileNotFound.htm"/>
</customErrors>
If the user request pages that doesn't exist ( like "www.example.com/NotExistPage.aspx" ) , user will be redirected to FileNotFound.htm page as we expect .
But if the user request some address like : "www.example.com/NotExistDirectory" without .aspx extension , the user will encounter IIS 7.5 Error page :
HTTP Error 404.0 - Not Found The
resource you are looking for has been
removed, had its name changed, or is
temporarily unavailable.
Detialed error information :
Module IIS Web Core
Notification MapRequestHandler
Handler StaticFile
Error Code 0x80070002
Requested URL http://www.example.com:80/NotExistDirectory
Physical Path D:\Websites\example\example.com\wwwroot\NotExistDirectory
Logon Method Anonymous
Logon User Anonymous
This is a yellow page which is not user friendly and we didn't expect .
I'm wondering setting customeError in webconfig doesn't support this type of address or not ? How can i prevent users seeing this yellow page .
Edit :
Thanks to David's answer , But I found the actual reason and correct solution. Please see my answer.
#Mostafa: I faced the exact same problem. I found out it can be solved by adding the following to the web.config file:
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" subStatusCode="-1" prefixLanguageFilePath="" path="/MyErrorPage.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
Thanks to #David solution, But the reason and complete solution are as below:
By setting customErrors mode to "On" it's only working when we get an exception in ASP.NET application, While when we trying to reach nonExistingdirectory Or notExsitingStaticResouce, IIS renders a 404 error and it does not reach to asp.net runtime and served by IIS directly.
So we need to add configuration for IIS as below in Web.config:
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="~/404.html" responseMode="File" />
</httpErrors>
<system.webServer>
It's important to set responseMode to "File", Otherwise status code automatically would change from 404 to 200. So from the client's perspective, they don't get the actual 404 status code.
This is because the ASP.Net module is configured to handle certain file extensions. IIS determines that .aspx must be handled by the ASP.Net module and then the customerrors section in the web.config ( and indeed web.config itself) kicks in.
Since you have requested a page not even configured for ASP.Net, IIS handles it on its own without passing the request on.
For any other files other than .aspx, you can configure this in IIS:
http://www.xefteri.com/articles/show.cfm?id=11
First, directory url must posess trailing slash, otherwise its just an extensionless file.
www.mysite.com/NotExistDirectory/
Second, ASP.net IIS module is handler for ASP MIME types only, so directory is leftover for web server.
Third, customerror is part of system.web is part of ASP.net configuration
and httperror is part of system.webserver is part of IIS configuration.
Assuming http module defaults in IIS configuration httperror will work with custom error for non-existing directory.
Here's my web.config customErrors section (you'll notice I've switched the mode to 'On' so I can see the redirect on my localhost):
<customErrors defaultRedirect="~/Application/ServerError.aspx" mode="On" redirectMode="ResponseRewrite">
<error statusCode="403" redirect="~/Secure/AccessDenied.aspx" />
</customErrors>
and here's the code that throws:
Catch adEx As AccessDeniedException
Throw New HttpException(DirectCast(HttpStatusCode.Forbidden, Integer), adEx.Message)
End Try
and here's what I end up with:
Which is not my pretty AccessDenied.aspx page but it is a forbidden error page so at least I know my throw is working.
I've removed the entry for 403 in IIS (7.0) as a desperate last attempt and unsuprisingly that made no difference.
I've run out of ideas now so any suggestions will be gratefully appreciated!
In fact, your aspx page may not be executing at all.
Go to IIS.
Go to your default website properties.
Click on the Home Directory Tab
Click the configuration button.
Now, check if a .aspx page is registered there.
You need to specify existingResponse="PassThrough" in the system.webServer section of the httpErrors element.
<configuration>
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
</configuration>