I've edited the webconfig in my Web Application (WebForms, .NET 4) adding this :
<customErrors mode="On" defaultRedirect="CustomError.aspx" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="CustomError.aspx" />
</customErrors>
now, If I request this page http://localhost/TestRedirect/asdasdasd (which it doesnt exist) I'd like to get the CustomError.aspx page (where I'll evalutate the page and redirect to the right source).
But in fact I get the HTTP 404.0 - Not Found. Unfortunatly I can't use httpErrors (due to my hosting permission), but maybe I can do it with customErrors? Or any other suggesions would be appreciated...
Your request is not coming to ASP.Net engine. Your customErrors tag will work fine if you use the url like
http://localhost/TestRedirect/asdasdasd.aspx
What version of IIS your webhosting provider has?
Related
In my application (Asp.Net web api) if the attacker inserts arbitrary text after in the api url then that text is displayed in the 404 error page. This can be used by attacker to trick the users to click on custom links or for phishing attacks.
I want to block any arbitrary text from showing up on error page like this. Point to note here, we have not implemented GUID based error messaging in application.
In order to set up a custom 404 error page add the following to web.config inside <system.web></system.web>:
<customErrors mode="On" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="~/404.html"/>
</customErrors>
I’ve set mode="On" so we can view the custom errors pages locally. Generally you would only want to display these in production so would set mode="RemoteOnly"
Then add also custom error pages in IIS (note that this only works in IIS 7+). In web.config add the following inside <system.webServer></system.webServer>:
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="/404.html" responseMode="File"/>
</httpErrors>
If it doesn't help, as I don't know the architecture of your application, you should go for a route matching solution:
Implementing a smart IHttpRouteConstraint
Applying the constraint to attribute routing
Applying the constraint to centralized routing
here are the details of the implementation:
https://www.strathweb.com/2014/10/route-matching-overriding-404-asp-net-web-api/
I have custom errors set in web.config for a 404 error and in IIS.
My custom error page works ok.
I want to be able to go to my custom error page from a page load event.
If I throw a 404 using
throw new HttpException(404, "Not found");
I get to the custom error page but looking at the responses I get a 302 found and a 301 permanent redirect then a 404 generated.
How can I remove the 302 and 301?
I have tried Response.Clear() before throwing the exception and also Response.StatusCode=404.
By default when there's an HTTP error IIS will redirect with a 302 to the error page.
You can tell IIS to instead rewrite the response with the defined error page in the web.config :
With the customErrors tag :
<system.web>
<customErrors redirectMode="ResponseRewrite">
<error statusCode="404" redirect="~/Error404.aspx"/>
</customErrors>
</system.web>
This works only with aspx pages, not with ASP.NET MVC
with the httpErrors tag :
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/Error404.aspx" responseMode="ExecuteURL"/>
</httpErrors>
</system.webServer>
With httpErrors you can't use ~/ to reference the root of the application, but it'll work with / if your application is not hosted as a sub application in IIS
I create a error page in iis for 503 error. Now i stop my website application pool. i got the other page service unavailable not my custom error page. I also try to config in web.config. It's not working please help me..
tried 1
<httpErrors errorMode="Custom">
<remove statusCode="503" subStatusCode="-1" />
<error statusCode="503" prefixLanguageFilePath="" path="/503.htm"
responseMode="ExecuteURL" />
</httpErrors>
tried 2
<customErrors mode="On">
<error statusCode="503" redirect="~/503.htm"/>
</customErrors>
if the site is stopped this means no instance is listening to requests to this site. thus it is not possible to do so by web.config.
If what you mean is to use a custom page for site maintenance, you can take use of the app_offline.htm file:
Taking an ASP.NET Site Offline with a Message
http://weblogs.asp.net/pleloup/archive/2008/09/22/taking-an-asp-net-site-offline-with-a-message.aspx
I've tried some ways to deploy my custom error page (e.g. error.aspx) onto my website:
Added customErrors: <customErrors mode="On"
defaultRedirect="error.aspx" />
Added httpError: <httpErrors>
<remove statusCode="404" subStatusCode="-1" /> <error
statusCode="404" prefixLanguageFilePath="" path="error.aspx"
responseMode="ExecuteURL" /> </httpErrors>
Currently, I can point this URL to my custom error page:
http://test.localdev.net/random_text.aspx
However, I failed for those kind of URL (unsupported extension), for these URLs IIS will use its own 404 page:
http://test.localdev.net/Default.random
Could anyone help me to fix it?
Thanks
are you using iis7 or later in integrated mode? Otherwise I think you need to configure error pages in iis itself.
I'd like all errors to go to the same error page. I did initially try the error tag with statuscodes. That works fine but I'd rather not have to specify so any statuscodes and still have every error go to one page. The problem is that if I remove the statuscode entries, a 404 goes to the server's 404 page and not my error page.
Is there some way to configure all errors to go to the same page?
You should change your web.config and add the customErrors element. You can then set the default redirect to a signle page
For Example
<customErrors mode="On" defaultRedirect="Error.aspx">
</customErrors>
You can refer to the MSDN article
http://msdn.microsoft.com/en-us/library/h0hfz6fc.aspx
On IIS7, you can use the new httpErrors section
<system.webServer>
<httpErrors defaultPath="Error.aspx" defaultResponseMode="Redirect">
</httpErrors>
</system.webServer>
For more information
http://www.iis.net/ConfigReference/system.webServer/httpErrors
If I understand well, you need to send all errors to your custom error page, and the errors that iis7 gives (not only your program)
This is something that you need to setup on II7, but you can also setup it from web.config (but actually give instructions to ii7) Here is an example
<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>
reference: http://www.iis.net/ConfigReference/system.webServer/httpErrors