Handling all errors within an asp.net application.

Posted by Sem Dendoncker on September 5th, 2010

Hello,

This post will describe how you can catch all errors in an asp.net application, process the error and finally show a nice page telling the users there was an error and the developers were informed about the issue.

For starters your application needs a Global.asax file.
To do this you just right click the website –> add new Item –> Global application class and press Add.

You will see some predefined methods in this file. We are interested in the “Application_Error” method.
This method is always executed after an error occurred.

Here’s the code you need to see the error, handle it and negate the error for the user.

void Application_Error(object sender, EventArgs e)
{
	StringBuilder message = new StringBuilder();

	if (Request != null)
	{
		message.AppendFormat("Error url: ", Request.Path);
	}

	if (Server != null)
	{
		Exception ex;
		for (ex = Server.GetLastError(); ex != null; ex = ex.InnerException)
		{
			// add everthing you want from the exception to the message.
			message.Append(String.Format("Exception: {0}", ex.InnerException));
			message.Append(String.Format("Stacktrace: {0}", ex.StackTrace));
		}
	}

	// You can retrieve any information as you would in a page.
	// Ex: Session data, Cookie Data, etc ...
	// All this data can be added to the message.

	// Mail the message to the developers (this way they now something went wrong)
	// This is just an example of a custom made service for bug reporting.
	BugReportingService service = new BugReportingService();
	service.Message = message.ToString();
	service.ReportToDevelopers();

	// This will clear the error (this way the users will not see the error).
	Server.ClearError();

	// Finally redirect the user to a custom made (user friendly) error page.
	Response.Redirect("~/FriendlyErrorPage.aspx");

}

That’s all there is to it.
Off course the above is just an example you can modify it in so many different ways I could write 20 pages about it.

Cheers,
Sem

Cookiemanager (C#)

Posted by Sem Dendoncker on June 11th, 2010

Hello,

In many of my applications I need to transfer the data from one page to another.
I used to do this by using a session but the thing with sessions is that they are unreliable.
You never know when they will disappear. There are several possibilities that a session can disappear (session timeout, application needs more memory, …).

The I thought about another way to do this. Then I thought why not using encrypted querystrings but that idea was countered real hard on stackoverflow.com (http://stackoverflow.com/questions/2989703/how-can-you-secure-encrypt-your-querystring-in-asp-net).

Finally I used one of the suggested solutions, why not putting your essential data into a cookie.
Therefore I’ve created an object called “CookieManager”.

Basically it works as a dictionary that contains all the properties you like/want.
You can add/remove properties (with their values) very easely.

This is the code to add a property

CookieManager manager = new CookieManager(cookieName, true);
manager.SetPropertyValue(property, value)

This is the code to get the value of a property

CookieManager manager = new CookieManager(cookieName, true);
manager.GetPropertyValue(property);

This is the code to remove a property

CookieManager manager = new CookieManager(cookieName, true);
manager.RemoveProperty(property);

Easy no?

I’m still working on a full manual for the manager, but the object is so easy to use anyone could use it.
For the ones who would like to have the sourcecode, just leave a comment and I’ll email it to you.

You can download the dll here

Cheers,
M.

Upgrading to enterprise library 5.0 makes the attribute validators fail.

Posted by Sem Dendoncker on June 10th, 2010

Hi,

Today I’ve upgraded my microsoft enterprise library 4.1 version to the 5.0 version.
After recompiling I got the error: “Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeValidator is not an attribute class”.
The first thing I thought was “Now what”!!

Fortunatly I have found the solution for this problem.
The only thing you need to do is to add the reference “System.ComponentModel.DataAnnotations” to your project and all errors will disappear.

I’m very pleased with the upgrade.
Keep it up!

Cheers,
Sem


Copyright © 2007 Sem Dendoncker. All rights reserved.