Custom http error pages for images? - asp.net

I know it's common for pages to redirect to a custom error page for 404 errors and such. But say somebody opens the url in their browser for an image that doesn't exist for example http://mysite.com/nothinghere.gif. This site returns a 404 error but it's not just the browser default, it's a custom 404. How do you configure an ASP.NET site on IIS7 to do this? the customerrors section in the web.config doesn't seem to apply to things like images, css, js, etc

customErrors section would apply to resources managed by ASP.NET. IIS7 has its own httpErrors section under webserver node. Of course, there is also UI to configure it. Anyway error handling behavior also changes based on you are running your ASP.NET application under classic mode or integrated mode. I will suggest that you read following articles to get hold of it.
http://www.braintrove.com/article/46 - this will tell you how to set up IIS7's own cutom error pages
Below explains IIS7 errors handling
http://learn.iis.net/page.aspx/267/how-to-use-http-detailed-errors-in-iis-70/
http://blogs.iis.net/ksingla/archive/2008/02/18/what-to-expect-from-iis7-custom-error-module.aspx

Make your custom error page a script.
When its called you can either send a html response for a page missing or query the request to find out the image name and then send an image or redirect to an image.
try these links for issues with custom 404 errors
IIS 404 Custom Error not working as expected
http://forums.asp.net/p/1603843/4089618.aspx

Related

How to override page render for an error page?

We have an ASP.NET MVC application, and when it encounters an error you are not redirected to an erro rpage, instead the content of that page is replaced with the content of the error page. You go fix your code and press refresh and you're done.
We have another application that's written in WebForms and I'd like to get the same behavior out of it. Right now the current behavior is that when an error occurs you get redirected to ~/Error.aspx. Is it possible to make webforms behave this way? Perhaps override the page render event somehow?
This is just for development right? Displaying the error.aspx on production is much better both from a security and user experience perspective.
In order to turn off custom errors, you need to know how to turn them on. Yes? There are several places custom errors can be configured.
in IIS Create a Custom HTTP Error Response (IIS 7)
an error handler on the page, in global.asax, or in a class defined elsewhere (app_code folder perhaps)
in web.config Web.config customErrors mode
Web.config is probably the most common place. Start there.

IIS 7.5 Logs and ASP.Net 404 Custom Pages

I want to make a custom 404 page for my site but I want to log all the normal information into my IIS 7.5 logs like the default 404 deals with so my Statics program can tell me things like what page got the 404 error, what was the referring URL to that broken page, and more. Do I have to do anything special on my 404 page to do this or has ASP evolved enough to automatically do logging for me if I return a 404 status code?
How you handle the errors determines how the errors will show up in the logs/responses. If you simply use custom error pages, it will show up no different in the log than if you had used the OOTB pages. If, however, you are writing an ASP.NET application, and handle/bury the exception, nothing will show up in the logs.
If you are writing a .NET application, this blog post provides a pretty good overview on how to properly handle errors for SEO.
ASP.NET Custom Error pages can be implemented in many different ways. The "worst" way are those that return a 301 redirection to "NotFound.aspx" (or similar), which will, of course, return a 200 status code. Unfortunately the IIS Manager actually lets you specify this method. If you're finding your 404s and 500s aren't being logged then check out this setting first.
Error pages, regardless of their implementation, must not issue any redirection and must set the status code to 404, that way IIS's logger will log it accordingly (IIS inspects the headers of all outgoing responses and uses that to populate the log).

IIS7 Custom Error URL Redirects Not Working with non-physical files

So here's the issue. We run Sitecore, which does URL rewriting, and allows for something like example.com/Folder/Page.aspx to be a proper URL.
Now, "Folder" doesn't actually exist on the file system, and neither does "Page.aspx". But those URL's work, because ASP.NET does what it's supposed to.
Okay, so now say I try to go to a web page that doesn't exist like example.com/idontexist.html. This doesn't exist in on the file system, and doesn't use ASP.NET to resolve the file, so then we get a IIS7 404 Error.
Now, I want to change the 404 Error Page to a Custom Error Page, by using a URL. Say, the URL is example.com/ErrorPage.aspx. This page is actually, not a physical page, but an item in Sitecore.
In the Custom Error configuration in IIS Manager, I am trying to change the 404 Error to point to a "URL Redirect" and I'm using "/ErrorPage.aspx" as the URL redirect. When I try to then go to a URL that doesn't exist, IIS7 blows up with an error saying that it can't find /ErrorPage.aspx.
In IIS6, this isn't an issue, and everything works fine. But on IIS7, I redirect to a URL that isn't physically on the server.
Help!
to make a very long story short, here's the thing:
1) configure IIS so that 404 errors go to "default.aspx"
2) create a page that actually handles what you want to handle.
Reference links, SDN originals:
http://sdn5.sitecore.net/upload/sitecore6/handling_http_404_a4.pdf
http://sdn.sitecore.net/upload/sitecore6/dynamic_links_a4.pdf

404 Redirect to Sitecore page allows for Extensionless URL

I am using Sitecore as a CMS solution. Recently, we decided to add redirect capability to our website for errors. In Sitecore, I have created two content pages, one to handle Server errors and another to handle 404 errors. I implemented the server error with basic sublayouts without any code behind. On the other hand, the 404 page had a sublayout that allows to display the link that was typed that resulted to the 404 error. I then proceeded to add these pages into the web.config file (I edited the customErrors setting to redirect to the Server errors page and the 404 errors to redirect to the 404 error page. I also replaced the NoItems settings to point to my 404 error page.
In addition, I changed the HTTP Error 404 to point to my new 404 error page in IIS (5.1). Lo and behold, my changes worked; I was able to redirect many of the errors to these new pages. However, what I found out is that by forcing IIS to redirect to my aspx page for 404 errors, My website now features extensionless URL redirecting. (Previosly if I typed http://mywebsite/foo, I will get a 404 error. Now after doing all of this, it gets me to the foo.aspx page)
Can anyone explain this behavior? Any plausible explanation will let me sleep at night.
Should I go ahead and use this as a temporary solution for Extensionless URL redirecting? What could be wrong with this solution?
Look for this in the web.config in the linkManager section:
addAspxExtension
Is that set to true? If so, then this is a sitecore setting you would have to change.

Custom URL Extensions/Routing Without IIS Access

I have a need to use extensionless URLs. I do not have access to IIS (6.0) so I cannot map requests to ASP.NET and handle with a HttpHandler/HttpModule. However, I can set a custom 404 page via web host control panel.
My current plan is to perform necessary logic in the custom 404 page, but it "feels wrong". Are there any recommendations that I am missing?
Edited: Added "Without IIS Access" to the title since someone thought this was a repeat question.
Without access to IIS, that would be your only option.
The 404 page really is your only option if you can't map the requests. I've seen several blog packages that do this to enable magic URLs like .../archive/YYYY/MM/DD and such - there's no such page, so it hits the 404 page and the 404 page does the redirection.

Resources