Handle unhandled exceptions in asp.net - 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'.

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.

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!

Unhandled exception was generated

I have a website in which some pages are in ASP and some pages are in ASP.Net, But my problem is
when i execute it on my Local PC then it is working
But when I execute it from Our Server then Got Error " unhandled exception was generated during the execution of
the current web request. Information regarding the origin and location of the exception can be identified using
the exception stack trace below."
and This i come in only ASP.Net Pages.
Help me,
Thnx in Advance
This is often caused by lack of permissions to write to the file system on the web server, but it can really be caused by anything that differs from your development environment.
There is an error/exception available somewhere that will help you diagnose and correct the problem and you need to find that error. One place to look is the Windows Event Log. Another thing to check is how your web.config file sets the value for CustomErrors.
Are you seeing something like this at all?
http://www.codinghorror.com/blog/images/yellow-screen-of-death-large.png

How do you do Error handling (especially SQL) in an ASP.NET project right

I have a another ASP.Net web project that I am working and I have approached error handling from different angles. I was just wondering if any seasoned and experienced developers out there had any recommendations.
Bonus points for discussions on a nice logging framework for ASP.net:)
I've used the ELMAH, (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable, on several projects and it works great:
http://code.google.com/p/elmah/
I've also used the built in Health Monitoring in ASP.NET 2.0:
http://msdn.microsoft.com/en-us/library/ms998306.aspx
It depends on the environment for which your application is targeted. But what I do is have a generic ErrorHandler.aspx page that I redirect to in the customErrors section of the web.config, and then any exception that I don't handle and hide causes the page to redirect to the error handler which lets the user know what happened and gives them the option to report it.
I would suggest deriving from System.Exception to make something that allows you to specify more specifics for the ErrorHandler.aspx page such as a FriendlyMessage property.
I use the Application_Error() event to grab unhandled exceptions. I write them to a log using the Application.Log() method, then I redirect them to a user-friendly error page.
The Application.Log() can be configured using the web.config file to direct the output to a file, the event log on the server, etc.
In some circumstances, I have also implemented my own logging using a generic TraceSource and custom Listener that logged the messages to a database table.

asmx wsdl loading forever

I have a web service application which has suddenly stopped working. I have enabled directory browsing in IIS, and can view the application directory. I can view the xml files within the application directory, but I cannot view the .config files, nor can I view the wsdls of any of the web services. When I try to browse to http://server/app/service.asmx?wsdl, the browser stays in the "loading" state forever.
Any ideas what might have gone wrong here?
Have you looked in the server's event log? Many ASP.NET errors will end up there, this sounds like it might qualify
Regarding the first question about loading, are you able to browse to the site on the server?
IIS protects Web.config files by default and will typically return a "This type of page is not served" error message. This is a good thing which will keep your connection strings and what not from being casually viewed. There is more information about allowing exceptions for this here.

Resources