How to override page render for an error page? - asp.net

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.

Related

How to get rid of YSOD but keep the standard logging functionality?

The default options of <system.web><customErrors> don't work very well in an ASP.NET MVC app if you want to return the actual HTTP status codes like 404, 500 etc. instead of 302 (because it works only with Web Forms).
No matter where and how I catch exceptions (like in Global.asax or in a global error handler) and "swallow" the exception (e. g. using Server.ClearError()), I have full control of what is rendered to the client but I loose the default logging (Web Events written to the Windows Event Log). This kind of logging works fine for me, it's very robust and I have Event Log monitoring anyway, so that's exactly what I want for logging.
Is there anything I can do to e. g. in Global.asax to simply force ASP.NET to log the current error as a Web Event and then clear the error?
A solution that requires ASP.NET 4 and IIS7.5 Integrated Mode would be fine.
One option that kind of works would be the use of <system.webServer><httpErrors>, however this works only with existingResponse="Replace", not Auto, because ASP.NET seems to automatically set TrySkipIisCustomErrors=true, and I would need this on Auto since a few controllers of my application need to return custom error pages (XML).
Thank you very much in advance!

ScriptResource not loading

This is an interesting problem...
I've got an ASP.NET 3.5 web application. It's relatively simple - a content page based on a master page, with a couple of usercontrols on the page.
There are a total of four controls on the page; two are custom controls and two are Ektron CMS controls.
For some reason, I've been getting 'Sys is undefined' Javascript errors. The Firebug error console also throws an "ASP.NET client framework failed to load" error. I can't figure out why this is happening.
The kicker - if I take the controls that don't work completely out of the content page, they work just fine. It's obviously something in the content page trashing the framework, but I haven't been able to figure this one out.
I've looked through several dozen articles before posting, including suggestions like:
Repairing the .NET framework (didn't work)
Various web.config tweaks (either already there or didn't work)
regiis -i command (I don't think this will help, since the controls do work when I'm not using the content page)
Commenting out custom scripts on the user controls (didn't work)
Commenting out the other user controls on the page (didn't work)
Create a blank page and drop only the control I'm interested in on the page (this actually works)
Has anyone seen something like this before? It's got to be something in that content page, but I'm struggling with what.
EDIT
I noticed while stepping through the code the UserControl's PAGE_LOAD event fires three times; on the third time the code throws an error. (I'm still trying to get my head around the error that's being thrown)
EDIT
I wanted to add this in case someone else sees this error. It turns out the problem was being caused by a Scriptmanager on the page template that wasn't needed.
When I took the Scriptmanager off the page, everything worked fine and the framework load error went away. I figure this problem must have broken other scripts and prevented them from running later on down the page.
If you get a 404 on ScriptResource.axd, there must be something wrong with your ASP.NET installation. I had this before, when I did not configure the correct ASP.NET pool. For instance: there could be another ASP.NET application on the same server assigned to the same pool, that is running under ASP.NET 2.0. You say your application runs under 3.5. Can you try iisreset on your machine and then start your application?
The pool could be assinged to the wrong .NET version.
Another option could be, that .NET 3.5 is not installed on the server at all.
Are you sure that the site runs well without your content page? ScriptResource.axd is a dynamic handler to generate JavaScript on the fly. If it's not available, the scripts cannot work.
Your use of Sys needs to be AFTER your ScriptManager is setup on the page. The ScriptManager is what instantiates the Sys object.

Handle unhandled exceptions in asp.net

How can we prevent page crash in asp.net? Is there any generic function or place like global.asax where we specify a file to redirect to when an unhanded exception occurs? (like we redirect to a specified page when 404 page not found exception occurs?
<customErrors> doesn't prevent your pages from "crashing". It just allows you to apologize to your users when the page does crash.
You should be examining the Application event log for messages from the source "ASP.NET <version>". These will include details of any exceptions your code has been generating and not handling. You need to fix these, as every one of these indicates that your users were not able to do what they came to your site to do.
You can do it in Global.asax or you can set up custom error pages in web.config.
ASP.NET Custom Error Pages
How can we prevent page crash in asp.net?
As Mal said, "If it crashes, you crashed it". The only way to "prevent" crashes is by carefully writing your code, testing, etc, as with any program.
To display a nice error page, check out the <customErrors> element in your web.config. MSDN docs here. Plenty of articles around if you google for 'customErrors'.

ASP.NET HttpHandler and WebResource.axd issues

I need a simple HttpHandler to handle specific non-existant paths in my ASP.NET project. I'm using sitefinity 4. I wrote the handler but whenever I try to run the site, I get a frustrating error
The WebResource.axd handler must be registered in the configuration to process this request.
I was putting the handler in the system.web part of the config but it looks liek VS2010 still uses IIS 6 for it's built in web server so I went ahead and switched it to use IIS 7 (local) and moved the registration of the handler to system.webServer and it works when I hit a non existant url but if I try to go to the site normally (valid url) I get that dang error again.
It worked once, giving me the correct site on a valid url but now it just continues to give me trouble.
How can I resolve this issue? I don't want to add that entry to the config as it wasn't there when I created the project and it only started when I added my handler.
EDIT: Only happens when I use Path="*" so how do I do a wildcard? I don't want to map a handler to catch a 404 page.
Froget it. No one seems to know even though I know i'm not the only one who has needed to do this. I got 404 to work but ONLY when I request a file, not a folder so thats a bust.
I found that Global.asax will hit under integrated mode so I just moved my code to the Application_BeginRequest() and it's working just fine. If anyone else has a better answer I will give them credit.
for iis7 and iis7.5, handlers are registered in system.webserver. the httphandlers and httpmodules in system.web are ignored and are used for IIS 6 and classic mode.
i hope this was helpful!

Custom http error pages for images?

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

Resources